2016-11-29 87 views
3

我目前正在尝试通过遵循官方Firebase文档来实现Firebase云消息传递。iOS:Firebase令牌返回null

我已经解决了启用了推送通知的证书和供应配置文件。我还使用CocoaPods安装了所有必需的框架(FirebaseMessaging,Firebase),并且它们似乎工作正常。

在应用程序委托中,我尝试使用下面的代码初始化Firebase云消息传递令牌。

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

预先感谢您的任何建议/帮助。

+0

你叫'FIRApp.configure()'吗? – chengsam

+0

我做到了。在尝试使用Firebase云消息传递之前,我已经将Firebase Analytics嵌入到我的应用程序中,并且运行良好。 – sabrinazuraimi

+1

您是否添加了用于检索最新令牌的密钥'firInstanceIDTokenRefresh'的观察者?第一次调用FIRInstanceID.instanceID()。token()!'将返回nil。 – chengsam

回答

3

已经回答不执行注释:

请务必按照安装向导上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) 

当你的令牌被刷新,它会调用:

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

对于那些谁仍然有上得到令牌的问题,也许是因为令牌尚未产生。

在引导(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.

我希望它能帮助。