iOS:Firebase token 返回 null

标签 ios swift firebase firebase-cloud-messaging

我目前正在尝试按照官方 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/

相关文章:

ios - 如何使用 NSRange 创建 NSArray 的子数组?

ios - 跨多个 View 保持同步事件的最佳实践或设计模式是什么?

ios - 如何快速在MKMapview上显示MKPinAnnotationView

firebase - 如何使用云功能通过签名下载url删除存储图像?

ios - 如何知道 UIViewController 的 subview 何时被删除?

ios - Facebook App Invide V4.0 通知

swift - 如何在 swift 中为服务器登录创建正确的完成处理程序?

javascript - Firebase auth onCreate 将自定义用户数据添加到实时数据库

ios 手动安装 firebase,出现错误 "firebase.h"找不到文件

ios - iOS 中 UIView 的垂直视差滚动