我目前正在尝试按照官方 Firebase 文档实现 Firebase 云消息传递。
我已经在启用推送通知的情况下设置了证书和配置文件。我还使用 CocoaPods 安装了所有必要的框架(FirebaseMessaging、Firebase),它们似乎工作正常。
在应用委托(delegate)中,我尝试使用以下代码初始化 Firebase 云消息传递 token 。
let token = FIRInstanceID.instanceID().token()!
我还将 GCM 设置为已启用,并且 GoogleService-Info.plist 中有一个 GCM 发件人 ID 值。
我得到的错误如下:
2016-11-29 16:11:12.358 Firebasesample[3852:122151] Firebase automatic screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable automatic screen reporting, set the flag FirebaseAutomaticScreenReportingEnabled to NO in the Info.plist
2016-11-29 16:11:12.419: Firebase messaging not setup correctly, nil senderID. fatal error: unexpectedly found nil while unwrapping an Optional value
预先感谢您的任何建议/帮助。
最佳答案
已在评论中回答但未实现:
确保按照 https://firebase.google.com/docs/cloud-messaging/ios/client 上的设置指南进行操作.
在您的 didFinishLaunchingWithOptions
中添加以下方法(在 FIRApp.configure()
之后)。
NotificationCenter.default.addObserver(self, selector: #selector(tokenRefreshNotification(_:)), name: NSNotification.Name.firInstanceIDTokenRefresh, object: nil)
当您的 token 刷新时,它将调用:
func tokenRefreshNotification(_ notification: Notification) {
guard let token = FIRInstanceID.instanceID().token() else {
QL3("No firebase token, aborting registering device")
return nil
}
//register your token somewhere..
registerToken(token)
}
关于iOS:Firebase token 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40859930/