独立watchOS应用集成FCM后收不到推送通知问题排查
独立watchOS应用FCM推送收不到的问题排查与解决
针对你的问题,需要手动处理FCM通知的接收与展示逻辑,你的代码里存在几个关键错误,同时还有一些配置和推送格式的要点需要注意:
一、代码中的明显错误
- 权限请求拼写错误:
center.requrestAuthorization应为center.requestAuthorization,这个错误会导致通知权限请求根本无法触发,手表从未真正获得展示通知的权限。 - 打印语句转义错误:
print("Set APNS Token\m")里的\m是无效转义符,应改为\n,虽不影响功能,但会导致控制台输出异常。 - 缺少UNUserNotificationCenter代理实现:未设置UNUserNotificationCenter的代理,也没有实现通知接收的处理方法,即使推送到达,系统也不知道如何展示通知。
二、修正后的完整代码示例
import SwiftUI import Firebase import UserNotifications @main struct Curiousfly_Watch_Watch_AppApp: App { @WKApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { AuthenticationView() } } } class AppDelegate: NSObject, WKApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate { func applicationDidFinishLaunching() { FirebaseApp.configure() let center = UNUserNotificationCenter.current() // 修正权限请求拼写错误 center.requestAuthorization(options: [.alert, .sound]) { granted, error in if let error = error { print("权限请求失败: \(error.localizedDescription)") return } if granted { print("通知权限已授权") WKExtension.shared().registerForRemoteNotifications() } else { print("用户拒绝了通知权限") } } // 设置通知中心代理,处理通知展示与交互 center.delegate = self Messaging.messaging().delegate = self } /// MessagingDelegate: 获取并处理FCM令牌 func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { guard let token = fcmToken else { return } print("FCM Token: \(token)") Messaging.messaging().subscribe(toTopic: "watch") { error in if let error = error { print("订阅主题失败: \(error.localizedDescription)") } else { print("成功订阅watch主题") } } } /// WKExtensionDelegate: 手动绑定APNS令牌到FCM func didRegisterForRemoteNotifications(withDeviceToken deviceToken: Data) { print("设置APNS Token成功") Messaging.messaging().apnsToken = deviceToken } /// UNUserNotificationCenterDelegate: 前台收到通知时的展示逻辑 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { // 前台时也展示通知(可根据需求调整) completionHandler([.alert, .sound]) } /// UNUserNotificationCenterDelegate: 用户点击通知后的交互逻辑 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { // 可添加跳转页面等自定义逻辑 completionHandler() } /// 处理远程通知注册失败的情况 func didFailToRegisterForRemoteNotificationsWithError(error: Error) { print("远程通知注册失败: \(error.localizedDescription)") } }
三、推送Payload格式要求
使用Postman发送FCM推送时,必须确保payload符合watchOS的APNS规范,示例JSON如下:
{ "to": "/topics/watch", "notification": { "title": "测试通知", "body": "这是一条watchOS推送测试" }, "apns": { "payload": { "aps": { "alert": { "title": "测试通知", "body": "这是一条watchOS推送测试" }, "sound": "default" } }, "headers": { "apns-priority": "5", // 后台推送用优先级5,即时推送用10 "apns-topic": "你的watchOS应用Bundle ID" // 必须与应用Bundle ID完全一致 } } }
- 重点:
apns-topic必须严格匹配你的watchOS应用Bundle ID,否则APNS会直接拒绝推送请求。
四、其他排查要点
- APNS密钥有效性:检查Firebase控制台中添加的APNS密钥是否属于当前开发者账号,且未过期。
- 手表网络状态:确保手表连接的Wi-Fi能正常访问外部网络(FCM推送依赖APNS,需要连接Apple服务器)。
- 环境匹配:开发环境需使用Firebase的开发服务器密钥,生产环境使用生产密钥,避免环境不匹配导致推送失败。
- 禁用自动Swizzling:watchOS上必须禁用FCM自动Swizzling,你手动绑定APNS令牌的操作是正确的,无需额外配置
GoogleService-Info.plist(默认已关闭Swizzling)。
内容的提问来源于stack exchange,提问作者Sayan Chakraborty
相关产品推荐
相关产品推荐

