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

iPhone无法接收FCM推送消息 求排查问题原因

iOS FCM推送接收失败排查方案

基础配置一致性校验

  • Bundle ID全链路匹配:确认以下三处的Bundle ID完全一致(大小写、特殊符号均需严格匹配):
    • Apple开发者平台的App ID、配置文件
    • Google Service Info.plist中的BUNDLE_ID字段
    • Xcode项目的Bundle Identifier
  • APNs凭据有效性:
    • 检查Apple开发者平台生成的APNs密钥是否已启用,且关联了当前App ID;若使用证书方式,需确认是推送专用证书(开发/生产环境对应),而非普通开发证书,且已正确上传至Firebase控制台
    • Firebase控制台的APNs配置需与测试包环境匹配(开发包对应开发证书/密钥,生产包对应生产证书/密钥)

代码实现正确性检查

  • AppDelegate.swift关键逻辑:
    • 必须实现设备Token传递逻辑:
      func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
          Messaging.messaging().apnsToken = deviceToken
      }
      
    • 初始化阶段需绑定Messaging代理:
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
          FirebaseApp.configure()
          Messaging.messaging().delegate = self
          // 注册远程通知
          UNUserNotificationCenter.current().delegate = self
          application.registerForRemoteNotifications()
          return true
      }
      
    • 实现Token更新监听:
      func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
          guard let token = fcmToken else { return }
          print("当前FCM Token: \(token)")
          // 建议上传至自有服务器保存
      }
      
  • main.dart中FCM逻辑:
    • 确认通知权限请求逻辑正确:
      final FirebaseMessaging messaging = FirebaseMessaging.instance;
      final NotificationSettings settings = await messaging.requestPermission(
        alert: true,
        badge: true,
        sound: true,
        provisional: false,
      );
      // 打印权限状态,确认已授权
      print("通知权限状态: ${settings.authorizationStatus}");
      
    • 配置消息监听(覆盖前台、后台场景):
      // 前台接收消息
      FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        print("前台收到推送: ${message.notification?.title}");
      });
      // 后台点击通知唤醒App
      FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
        print("点击后台推送进入App: ${message.notification?.title}");
      });
      

测试环境匹配校验

  • 推送环境与包环境一致:Xcode直接运行的开发包,需在Firebase控制台选择开发环境推送;Ad Hoc/TestFlight的生产包,选择生产环境推送
  • 单设备Token测试:通过日志获取当前设备的FCM Token,直接用该Token在Firebase控制台做单设备推送测试,排除批量推送的逻辑问题
  • 后台模式配置确认:Xcode项目Signing & Capabilities中,Background Modes必须勾选Remote notifications,同时Info.plist中UIBackgroundModes需包含remote-notification字段

其他排查点

  • 网络环境:iOS设备需能正常访问FCM服务(可尝试切换至海外网络测试)
  • 系统通知权限:在iPhone「设置-通知-你的App」中,确认已开启允许通知,且声音、横幅等子权限均已启用
  • Google Service Info.plist完整性:确认文件已正确添加至Xcode项目(勾选「Copy items if needed」),且GOOGLE_APP_ID、GCM_SENDER_ID等核心字段与Firebase控制台完全一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 10:12:51