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

求助:Firebase控制台发送含图片的Data Payload/富通知失败

解决方案:Firebase控制台发送带图片的富通知/ Data Payload

我之前也碰到过完全一样的问题!Firebase控制台的「附加选项」里直接加图片字段确实不会自动渲染成富通知,这里得先搞清楚控制台的两种消息类型逻辑,再对应调整配置:

1. 快速发带图通知:用Notification Payload的内置图片选项

如果你只是想发带图片的通知(不是纯Data Payload),根本不用去「附加选项」里加键值对!控制台的「通知内容」区域里就有专门的「添加图片」按钮(就在标题、输入框的下方),直接填入图片的公开URL(比如https://example.com/image.jpg),发送后客户端就能直接收到带图的富通知——这是FCM官方支持的Notification Payload渲染方式,简单省心。

2. 纯Data Payload的图片处理:客户端必须手动实现

如果你的业务需求是发送纯Data Payload(完全由客户端控制通知展示逻辑),那控制台「附加选项」里添加的图片字段(比如image、picture这类自定义键),FCM只会原样把数据传给客户端,不会自动帮你渲染成带图通知。这时候需要在客户端代码里手动读取这个字段,再构建带图片的通知:

Android示例代码(Kotlin)

class MyFirebaseMessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)
        
        // 从Data Payload中读取自定义字段
        val title = remoteMessage.data["title"] ?: "默认标题"
        val body = remoteMessage.data["body"] ?: "默认内容"
        val imageUrl = remoteMessage.data["image"]

        // 构建通知
        val builder = NotificationCompat.Builder(this, "YOUR_CHANNEL_ID")
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(title)
            .setContentText(body)
            .setPriority(NotificationCompat.PRIORITY_HIGH)

        // 处理图片(用Glide加载示例,也可以用Picasso)
        imageUrl?.takeIf { it.isNotEmpty() }?.let { url ->
            try {
                val bitmap = Glide.with(this)
                    .asBitmap()
                    .load(url)
                    .submit()
                    .get()
                // 设置大图样式
                builder.setStyle(NotificationCompat.BigPictureStyle().bigPicture(bitmap))
                // 设置通知图标
                builder.setLargeIcon(bitmap)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        // 发送通知
        NotificationManagerCompat.from(this).notify(System.currentTimeMillis().toInt(), builder.build())
    }
}

iOS示例代码(Swift)

extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        
        // 从Data Payload读取图片URL
        guard let imageUrlString = userInfo["image"] as? String, let imageUrl = URL(string: imageUrlString) else {
            completionHandler([.banner, .sound])
            return
        }
        
        // 下载图片并创建通知附件
        URLSession.shared.dataTask(with: imageUrl) { data, _, error in
            guard let data = data, error == nil, let tempFile = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.appendingPathComponent("notification_image.jpg") else {
                completionHandler([.banner, .sound])
                return
            }
            
            do {
                try data.write(to: tempFile)
                let attachment = try UNNotificationAttachment(identifier: "image", url: tempFile)
                let content = UNMutableNotificationContent()
                content.title = userInfo["title"] as? String ?? "默认标题"
                content.body = userInfo["body"] as? String ?? "默认内容"
                content.attachments = [attachment]
                
                let request = UNNotificationRequest(identifier: notification.request.identifier, content: content, trigger: nil)
                center.add(request)
                completionHandler([.banner, .sound])
            } catch {
                completionHandler([.banner, .sound])
            }
        }.resume()
    }
}

3. 混合消息的注意事项

如果你在控制台同时设置了Notification Payload(标题、正文)和Data Payload(附加字段),即使Data里加了图片,FCM还是会优先用Notification Payload的配置来展示通知——只有当客户端收到消息后,手动读取Data字段并替换通知内容时,图片才会显示。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:59:19