iOS中使用Firebase推送通知无法显示图片的问题排查
iOS Firebase推送通知图片不显示的排查方案
看了你提供的推送payload,结合iOS + FCM的推送逻辑,问题大概率出在payload格式、iOS端扩展配置这几个核心环节,我给你拆解下排查方向:
1. 推送Payload的格式不符合FCM图片展示要求
你把图片URL放在了data字段的attachment-url里,但FCM要让iOS显示通知图片,有两个关键前提:
- 必须在
apns.aps里添加mutable-content: 1,这个字段是告诉iOS允许通过通知扩展来修改通知内容(包括添加图片) - 图片信息要么通过FCM的
notification.image字段直接指定,要么保留data里的attachment-url,但需要配合后续的通知扩展来处理
给你一个修正后的payload示例:
{ "content_available": true, "apns": { "aps": { "alert": { "title": "你的通知标题", "body": "你的通知内容" }, "mutable-content": 1 // 这个是核心,必须加 } }, "data": { "actionId": "219CE497-1FAC-4974-B605-F28B87E756AB", "actionName": "display", "actionType": "Movie", "attachment-url": "http://singteltv.com.sg/_channels/pic_ch1.jpg", "dataId": "123fcm-20180327", "dataMessage": "test Swathi: !#@$%^&*' 中文 - ไทย 1234567890 1234567891 1234567892..." }, "notification": { "title": "你的通知标题", "body": "你的通知内容", "image": "http://singteltv.com.sg/_channels/pic_ch1.jpg" // 部分场景下可直接生效 } }
2. iOS端必须配置通知服务扩展(Notification Service Extension)
iOS默认不会自动下载并展示通知里的图片,必须通过通知服务扩展来手动处理图片的下载和附加操作,步骤如下:
- 在Xcode中给你的项目添加新Target,选择「Notification Service Extension」
- 在扩展的
didReceiveNotificationRequest:withContentHandler:方法中,从通知的userInfo里取出attachment-url,下载图片后创建UNNotificationAttachment,替换原通知内容
给你一段核心代码示例:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) guard let content = bestAttemptContent, let attachmentUrlStr = content.userInfo["attachment-url"] as? String, let url = URL(string: attachmentUrlStr) else { contentHandler(bestAttemptContent ?? request.content) return } // 下载图片并附加到通知 URLSession.shared.downloadTask(with: url) { tempLocation, _, error in guard let tempLocation = tempLocation, error == nil else { contentHandler(content) return } let tempDir = FileManager.default.temporaryDirectory let imageFile = tempDir.appendingPathComponent(url.lastPathComponent) do { try FileManager.default.moveItem(at: tempLocation, to: imageFile) let attachment = try UNNotificationAttachment(identifier: "notification_image", url: imageFile) content.attachments = [attachment] contentHandler(content) } catch { contentHandler(content) // 失败则展示原通知 } }.resume() }
3. 图片URL的兼容性问题
- 检查图片URL是否能正常访问:iOS 9+默认要求HTTPS链接,如果用HTTP,需要在
Info.plist里配置NSAppTransportSecurity的NSAllowsArbitraryLoads为YES(或针对特定域名配置例外) - 确认图片格式支持:iOS通知仅支持JPG、PNG等常见格式,确保你的图片能正常打开
4. 其他基础配置检查
- 确认App已经获取了完整的通知权限(包括
alert权限) - 检查Firebase的
GoogleService-Info.plist是否正确导入,FCM推送证书是否有效
内容的提问来源于stack exchange,提问作者SwathiK
相关产品推荐
相关产品推荐

