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

iOS端FCM的messaging didReceiveRemoteMessage方法未调用问题咨询

解决iOS FCM messaging:didReceiveRemoteMessage: 不触发的问题

我来帮你捋捋这个常见的FCM集成坑,咱们一步步排查:

1. 先确认Delegate是否正确绑定

这是最容易踩的第一个坑:

  • 如果你用的是iOS 13+的SceneDelegate,一定要在scene(_:willConnectTo:options:)方法里设置Messaging的Delegate,而不是只在AppDelegate的didFinishLaunchingWithOptions里设置(因为后台唤醒时AppDelegate的方法可能不会触发)。
  • 确保你的类已经遵守MessagingDelegate协议。

示例代码:

// SceneDelegate.swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let _ = (scene as? UIWindowScene) else { return }
    // 绑定Delegate
    Messaging.messaging().delegate = self
}

// 实现协议方法
extension SceneDelegate: MessagingDelegate {
    func messaging(_ messaging: Messaging, didReceiveRemoteMessage remoteMessage: MessagingRemoteMessage) {
        print("收到数据消息:\(remoteMessage.appData)")
        // 这里写你的数据消息处理逻辑
    }
}

2. 检查数据消息的Payload结构

FCM对纯数据消息的格式有严格要求:

  • 纯数据消息必须完全没有notification字段,如果同时包含notification和data,App前台时messaging:didReceiveRemoteMessage:会触发,但后台时系统会自动弹通知,点击通知后数据会通过didReceiveRemoteNotification传递,而非这个Delegate方法。
  • 正确的纯数据消息Payload示例(用FCM HTTP v1 API发送):
{
    "message": {
        "token": "你的设备Token",
        "data": {
            "order_id": "12345",
            "type": "payment_success"
        }
    }
}

3. 确认iOS通知权限与FCM基础配置

  • 必须请求远程通知权限,并且注册远程通知:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
    if granted {
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
}
  • 检查GoogleService-Info.plist是否正确添加到项目,Firebase控制台是否配置了正确的APNs Auth Key(FCM依赖APNs推送,这一步错了会导致消息根本传不到设备)。

4. 检查Xcode Capabilities设置

  • 确保Push Notifications Capability已开启。
  • 勾选Background Modes里的Remote notifications选项(即使是前台接收数据消息,这个配置也建议开启,避免系统拦截)。
  • 确认Provisioning Profile包含Push Notifications权限,且Bundle ID与Firebase控制台完全一致。

5. 测试环境的特殊注意事项

  • Debug模式下,如果你用Xcode调试App,偶尔会出现Delegate不触发的情况,建议断开Xcode连接,直接在设备上打开App再发送消息测试。
  • 发送消息时要匹配环境:Debug用Sandbox APNs,Release用Production APNs,Firebase控制台的测试消息默认用Sandbox,生产消息对应正式环境。

6. 更新Firebase SDK版本

旧版本的FirebaseMessaging可能存在Delegate不触发的bug,建议更新到最新版本:

# Podfile里确保依赖最新版
pod 'Firebase/Messaging'

然后执行pod update Firebase/Messaging。

先从Delegate绑定和Payload结构这两个最常见的点排查,一般就能解决问题了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:41:53