You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Firebase推送消息中禁用图片展开视图?

解决推送通知展开后图片过大的问题

iOS 端解决方案

方法1:提前压缩/裁剪图片

先将图片处理到合适的小尺寸(比如200x200),再作为通知附件添加,确保展开视图显示的是处理后的小图:

// 图片尺寸调整函数
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
    let size = image.size
    let widthRatio  = targetSize.width / size.width
    let heightRatio = targetSize.height / size.height
    
    // 按比例缩放,避免拉伸
    let newSize = widthRatio > heightRatio 
        ? CGSize(width: size.width * heightRatio, height: size.height * heightRatio) 
        : CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
    let rect = CGRect(origin: .zero, size: newSize)
    
    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
    image.draw(in: rect)
    let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    return resizedImage
}

// 生成通知附件
if let originalImage = UIImage(named: "notification_image"),
   let resizedImage = resizeImage(image: originalImage, targetSize: CGSize(width: 200, height: 200)),
   let imageData = resizedImage.pngData(),
   let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent("thumb.png") as URL? {
    try? imageData.write(to: tempURL)
    let attachment = try? UNNotificationAttachment(identifier: "notification_thumb", url: tempURL, options: nil)
    
    // 组装通知内容
    let content = UNMutableNotificationContent()
    content.title = "通知标题"
    content.body = "通知内容"
    content.attachments = [attachment!]
    
    // 后续创建通知请求的逻辑...
}

方法2:通过附件选项指定缩略图尺寸

无需修改原图,直接在创建UNNotificationAttachment时,用UNNotificationAttachmentOptionsThumbnailSizeKey指定缩略图尺寸:

let imageFileURL = Bundle.main.url(forResource: "notification_image", withExtension: "png")!
let thumbnailSize = NSValue(cgSize: CGSize(width: 200, height: 200))
let options = [UNNotificationAttachmentOptionsThumbnailSizeKey: thumbnailSize]

if let attachment = try? UNNotificationAttachment(identifier: "notification_thumb", url: imageFileURL, options: options) {
    let content = UNMutableNotificationContent()
    content.title = "通知标题"
    content.body = "通知内容"
    content.attachments = [attachment]
    
    // 后续创建通知请求的逻辑...
}

Android 端解决方案

如果使用BigPictureStyle,直接将小尺寸图片同时设置为bigPicture,并移除重复的大图标,即可避免展开时图片放大:

val smallThumbBitmap = BitmapFactory.decodeResource(resources, R.drawable.notification_small_thumb)

val notification = NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL_ID")
    .setSmallIcon(R.drawable.ic_notification)
    .setContentTitle("通知标题")
    .setContentText("通知内容")
    .setLargeIcon(smallThumbBitmap) // 初始显示的小图
    .setStyle(NotificationCompat.BigPictureStyle()
        .bigPicture(smallThumbBitmap) // 展开视图显示的小图
        .bigLargeIcon(null)) // 取消默认的大图标重复显示
    .build()

// 发送通知
NotificationManagerCompat.from(this).notify(1, notification)

如果是远程推送(如FCM),建议直接推送预先处理好的小尺寸图片URL,客户端接收后按上述方式构建通知,避免加载大图导致展开后显示过大。

内容的提问来源于stack exchange,提问作者LittleLebowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 02:35:24