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

Swift开发iOS应用集成Firebase Cloud Notification后通知打开点击数为0

FCM推送打开/点击数无法统计问题排查方案

1. 确认核心上报代码已正确实现

FCM不会自动统计通知点击事件,需要你在所有通知打开的触发场景主动调用上报接口,漏加对应逻辑是90%以上同类问题的根因:

  • 需调用的上报方法为 Messaging.messaging().appDidReceiveMessage(userInfo),必须在应用冷启动、后台、前台三种状态下的通知点击场景都触发调用

UIKit 生命周期代码示例

// 应用在前台/后台运行时,用户点击通知触发
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    // 上报点击事件给FCM
    Messaging.messaging().appDidReceiveMessage(userInfo)
    // 此处添加你的业务逻辑
    completionHandler()
}

// 应用处于杀死状态,用户点击通知冷启动触发
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // 初始化Firebase基础配置逻辑
    FirebaseApp.configure()
    // 上报冷启动通知点击事件
    if let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable: Any] {
        Messaging.messaging().appDidReceiveMessage(userInfo)
    }
    UNUserNotificationCenter.current().delegate = self
    return true
}

SwiftUI 生命周期代码示例

@main
struct YourApp: App {
    init() {
        FirebaseApp.configure()
        UNUserNotificationCenter.current().delegate = UIApplication.shared.delegate as? UNUserNotificationCenterDelegate
    }
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onReceive(NotificationCenter.default.publisher(for: UIApplication.didFinishLaunchingNotification)) { _ in
                    // 处理冷启动通过通知打开的场景
                    if let launchOptions = UIApplication.shared.launchOptions,
                       let userInfo = launchOptions[.remoteNotification] as? [AnyHashable: Any] {
                        Messaging.messaging().appDidReceiveMessage(userInfo)
                    }
                }
        }
    }
}

// 扩展实现通知点击回调
extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        Messaging.messaging().appDidReceiveMessage(userInfo)
        completionHandler()
    }
}

2. 确认基础配置无误

  • 确认集成的 Firebase/Messaging 版本 >= 8.0.0,低版本存在上报数据丢失的已知问题
  • 确认已正确将APNs Token同步给FCM,示例代码如下:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken
}
  • 不要修改/删除FCM推送默认携带的google.c.*系列参数,这些是FCM统计事件的唯一标识,修改后会导致上报数据无法匹配到对应推送记录

3. 统计延迟排查

FCM后台的打开/点击统计存在最长24小时的延迟,如果你是刚完成测试就查看报表,建议等待24小时后再确认数据是否正常更新。

4. 调试阶段验证上报是否成功

可以开启Firebase debug日志确认上报请求是否正常发送:

  1. 在Xcode的 Scheme > Run > Arguments > Environment Variables 中添加参数-FIRDebugEnabled,值设为YES
  2. 触发通知点击后,在Xcode控制台搜索Messaging: Successfully sent message to server for reporting字样,出现则说明上报成功,只需等待后台统计更新即可

内容的提问来源于stack exchange,提问作者Keshu R.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 20:24:03