简体   繁体   English

iOS:Firebase 令牌返回 null

[英]iOS: Firebase token returning null

I am currently trying to implement Firebase Cloud Messaging by following the official Firebase documents.我目前正在尝试按照 Firebase 官方文档实施 Firebase Cloud Messaging。

I have settled the certificates and the provisioning profiles with Push Notifications enabled.我已经在启用推送通知的情况下解决了证书和配置文件。 I have also installed all the necessary frameworks(FirebaseMessaging,Firebase) using CocoaPods and they seem to work fine.我还使用 CocoaPods 安装了所有必要的框架(FirebaseMessaging、Firebase),它们似乎工作正常。

At the app delegate, I tried to initialize the Firebase cloud messaging token with the code below.在应用程序委托中,我尝试使用以下代码初始化 Firebase 云消息传递令牌。

let token = FIRInstanceID.instanceID().token()!

I have also set GCM to enabled and there is a GCM sender id value in the GoogleService-Info.plist.我还将 GCM 设置为启用,并且 GoogleService-Info.plist 中有一个 GCM 发件人 ID 值。

The error that I am getting is as follows:我得到的错误如下:

2016-11-29 16:11:12.358 Firebasesample[3852:122151] Firebase automatic screen reporting is enabled. 2016-11-29 16:11:12.358 Firebasesample[3852:122151] Firebase 自动屏幕报告已启用。 Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name.调用 +[FIRAnalytics setScreenName:setScreenClass:] 设置屏幕名称或覆盖默认屏幕类名称。 To disable automatic screen reporting, set the flag FirebaseAutomaticScreenReportingEnabled to NO in the Info.plist要禁用自动屏幕报告,请在 Info.plist 中将标志 FirebaseAutomaticScreenReportingEnabled 设置为 NO

2016-11-29 16:11:12.419: Firebase messaging not setup correctly, nil senderID. 2016-11-29 16:11:12.419:Firebase 消息设置不正确,发送者 ID 为零。 fatal error: unexpectedly found nil while unwrapping an Optional value致命错误:在解开 Optional 值时意外发现 nil

Thank you in advance for any advice/help.预先感谢您的任何建议/帮助。

Was already answered in the comment without the implementation:在没有实现的情况下已经在评论中回答了:

Make sure you follow the setup guide on https://firebase.google.com/docs/cloud-messaging/ios/client .确保您遵循https://firebase.google.com/docs/cloud-messaging/ios/client上的设置指南。

In your didFinishLaunchingWithOptions add the following method (after FIRApp.configure() ).在您的didFinishLaunchingWithOptions添加以下方法(在FIRApp.configure() )。

NotificationCenter.default.addObserver(self, selector: #selector(tokenRefreshNotification(_:)), name: NSNotification.Name.firInstanceIDTokenRefresh, object: nil)

When your token is refreshed, it will call:当您的令牌刷新时,它将调用:

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)
}

For those who still having problems on get the token, maybe is because the token has not yet been generated.对于那些在获取令牌方面仍然存在问题的人,可能是因为令牌尚未生成。

On the guide( https://firebase.google.com/docs/cloud-messaging/ios/client ) you can find this part:在指南( https://firebase.google.com/docs/cloud-messaging/ios/client )上,您可以找到这部分:

When you need the current token, retrieve it.当您需要当前令牌时,检索它。 This may return null if the token has not yet been generated.如果尚未生成令牌,则这可能返回 null。

I hope it helps.我希望它有帮助。

I was stuck with this issue, was not receiving firebase push notification on the IOS device ABOVE IOS-10 because firebase token might be longer than the data type you chose it to be stored with.我被这个问题困住了,没有在 IOS-10 以上的 IOS 设备上收到 firebase 推送通知,因为 firebase 令牌可能比您选择用于存储的数据类型长。 Check your datatype for the token column.检查令牌列的数据类型。 I Have converted from varchar to text and it works and saved our day.我已经从 varchar 转换为文本,它可以工作并挽救了我们的一天。

Regards, Gurminder问候, Gurminder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM