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

Appcelerator iOS富推送经FCM发送时图片不显示问题咨询

解决iOS FCM富推送图片不显示的问题(Titanium环境)

我来帮你搞定这个问题!既然你已经添加了UNNotificationServiceExtension但还是只能收到普通通知,大概率是Payload格式不对,或者Extension的代码/配置有遗漏,咱们一步步来排查:

一、先把FCM Payload改对!

这是最容易踩坑的地方,你的Payload必须同时满足两个条件:开启通知可修改,并且把图片URL放到Extension能拿到的字段里。给你一个能直接用的示例:

{
  "to": "e4DgI95lsPA:APA91bHFX9MUmJ...",
  "notification": {
    "title": "我的富推送",
    "body": "这是带图片的推送内容",
    "mutable_content": true
  },
  "apns": {
    "payload": {
      "aps": {
        "mutable-content": 1
      },
      "mediaUrl": "https://your-domain.com/your-image.jpg"
    }
  }
}

划重点:

  • notification里的mutable_content(下划线)和apns.payload.aps里的mutable-content(短横)都要加,FCM和APNS的字段格式要求不一样,缺一不可
  • 图片URL要放在apns.payload下的自定义字段(比如mediaUrl)里,这样Extension才能读取到

二、检查你的UNNotificationServiceExtension代码

Extension的核心任务就是下载图片并把它添加到通知附件里,确保你的代码逻辑没问题。这里给你一个Swift版本的参考实现(如果是Objective-C可以对应调整):

import UserNotifications

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = request.content.mutableCopy() as? UNMutableNotificationContent
        
        guard let content = bestAttemptContent else {
            contentHandler(request.content)
            return
        }

        // 从payload里取出图片URL
        if let mediaUrlStr = content.userInfo["mediaUrl"] as? String, let mediaUrl = URL(string: mediaUrlStr) {
            // 下载图片到临时目录
            URLSession.shared.downloadTask(with: mediaUrl) { tempLocation, _, error in
                if let error = error {
                    print("图片下载失败: \(error.localizedDescription)")
                    contentHandler(content)
                    return
                }

                guard let tempLocation = tempLocation else {
                    contentHandler(content)
                    return
                }

                // 移动临时文件到可访问的路径
                let tempDir = NSTemporaryDirectory()
                let fileName = mediaUrl.lastPathComponent
                let targetUrl = URL(fileURLWithPath: tempDir).appendingPathComponent(fileName)
                
                do {
                    try FileManager.default.moveItem(at: tempLocation, to: targetUrl)
                    // 创建通知附件并添加到内容里
                    let attachment = try UNNotificationAttachment(identifier: "image_attachment", url: targetUrl, options: nil)
                    content.attachments = [attachment]
                } catch {
                    print("处理图片附件失败: \(error.localizedDescription)")
                }

                // 交付修改后的通知
                contentHandler(content)
            }.resume()
        } else {
            // 没有图片URL,直接交付原通知
            contentHandler(content)
        }
    }

    override func serviceExtensionTimeWillExpire() {
        // 超时的时候,交付当前能处理的最佳内容
        if let handler = contentHandler, let content = bestAttemptContent {
            handler(content)
        }
    }
}

还要检查Extension的配置:

  • 打开Extension的Info.plist,确认NSExtension下的NSExtensionPointIdentifier是com.apple.usernotifications.service
  • 如果你的图片是HTTP链接,要在Extension的Info.plist里添加App Transport Security配置,允许非HTTPS请求(当然最好还是用HTTPS):
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

三、Titanium项目的额外配置

因为你用的是Titanium的FCM模块,还要确认这些点:

  • 模块版本是最新的,避免兼容性问题
  • 在tiapp.xml里已经开启了推送的后台模式:
<ios>
    <plist>
        <dict>
            <key>UIBackgroundModes</key>
            <array>
                <string>remote-notification</string>
            </array>
        </dict>
    </plist>
</ios>
  • 确认设备已经成功注册FCM,你用的to字段的token是有效的

四、测试小技巧

  • 直接用FCM控制台发送测试推送,按照上面的Payload格式填写,快速验证是否有效
  • 打开Xcode的控制台,过滤NotificationService关键词,看看有没有下载或处理图片的错误日志
  • 手动在浏览器里打开图片URL,确认它能正常访问,设备网络也没问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:15:44