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

独立WatchOS项目集成FCM推送无响应问题求助

独立WatchOS项目FCM推送接收失败排查方案

一、代码错误修复

1. 修正MessagingDelegate方法参数

你的messaging(_:didReceive:)方法参数类型错误,导致数据消息无法被正确处理,需将参数改为MessagingRemoteMessage:

// 错误写法
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingDelegate) {
    print("Received data message: \(remoteMessage.description)")
}

// 正确写法
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    print("Received data message: \(remoteMessage.appData)")
    // 若需手动触发通知展示,可在此构造通知请求
    let content = UNMutableNotificationContent()
    content.title = "新消息"
    content.body = remoteMessage.appData["question"] as? String ?? ""
    content.sound = .default
    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
    UNUserNotificationCenter.current().add(request)
}

2. 移除重复的通知注册调用

applicationDidFinishLaunching中重复调用了registerForRemoteNotifications(),保留授权回调内的调用即可:

func applicationDidFinishLaunching() {
    FirebaseApp.configure()
    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound]) { granted, error in
        if granted {
            WKExtension.shared().registerForRemoteNotifications()
        } else {
            print("通知授权失败: \(error?.localizedDescription ?? "未知错误")")
        }
    }
    Messaging.messaging().delegate = self
    // 移除此行重复调用
    // WKExtension.shared().registerForRemoteNotifications()
}

二、修正FCM推送Payload格式

你的Payload存在结构错误,data字段不应嵌套在notification内,且针对WatchOS需补充APNS专属配置:

{
    "data": {
        "questionId": 26354,
        "messageType": "textMessage",
        "question": "Test-Message",
        "answer": "[]",
        "caregiverName": "Test"
    },
    "content_available": true,
    "to": "你的Watch端FCM Token",
    "apns": {
        "payload": {
            "aps": {
                "alert": {
                    "title": "测试通知",
                    "body": "Test-Message"
                },
                "sound": "default"
            }
        },
        "priority": 10 // 高优先级确保通知立即推送
    }
}

关键点:

  • data需与apns同级,不能嵌套在notification中
  • 独立WatchOS依赖APNS,必须在apns.payload.aps中明确配置通知内容
  • 测试阶段用priority:10,避免低优先级被系统延迟推送

三、配置项检查

1. Firebase控制台配置

  • 确认已添加WatchKit Extension对应的应用,Bundle ID为io.watchrx.watch.watchkitapp.watchkitextension
  • 将该应用的GoogleService-Info.plist添加到WatchKit Extension目标(而非WatchKit App目标)
  • 上传包含WatchOS Bundle ID的APNS证书到Firebase控制台

2. WatchOS项目配置

  • 在WatchKit Extension的Info.plist中添加UIBackgroundModes,包含remote-notification项
  • 在WatchKit Extension的Signing & Capabilities中开启Push Notifications和Background Modes(勾选Remote notifications)

四、额外排查点

  • 确认测试用的FCM Token是从Watch端获取的,而非iPhone端Token
  • 在didRegisterForRemoteNotifications(withDeviceToken:)中添加错误捕获,检查注册是否失败
  • 测试时让Watch处于解锁状态,且与iPhone断开连接(确保进入独立WatchOS模式)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 10:02:10