APNS Firebase通知无法获取令牌

对于Swift3 / iOS10,请看这个链接:

ios10,Swift 3和Firebase推送通知(FCM)

我正在尝试使用Firebase for Notifications,并按照文档中的说明完全整合。 但我不明白为什么不行。 当我build立我的项目,我看到这一行:

2016-05-25 16:09:34.987: <FIRInstanceID/WARNING> Failed to fetch default token Error Domain=com.firebase.iid Code=0 "(null)" 

这个我的AppDelegate:

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { FIRApp.configure() FIRDatabase.database().persistenceEnabled = true var service: DataService = DataService() service.start() registerForPushNotifications(application) application.registerForRemoteNotifications() return true } func registerForPushNotifications(application: UIApplication) { let notificationSettings = UIUserNotificationSettings( forTypes: [.Badge, .Sound, .Alert], categories: nil) application.registerUserNotificationSettings(notificationSettings) } func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) { if notificationSettings.types != .None { application.registerForRemoteNotifications() } } func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { let tokenChars = UnsafePointer<CChar>(deviceToken.bytes) var tokenString = "" for i in 0..<deviceToken.length { tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]]) } FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown) print("Device Token:", tokenString) } func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { // Print message ID. print("Message ID: \(userInfo["gcm.message_id"]!)") // Print full message. print("%@", userInfo) } 

1.在didFinishLaunchingWithOptions方法中设置通知观察者

2.设置tokenRefreshNotification方法,然后在这个方法中获得Token。

见下面的代码

 import Firebase import FirebaseMessaging override func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { FIRApp.configure() NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification(notification:)), name: NSNotification.Name.firInstanceIDTokenRefresh, object: nil) } // NOTE: Need to use this when swizzling is disabled public func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox) } func tokenRefreshNotification(notification: NSNotification) { // NOTE: It can be nil here let refreshedToken = FIRInstanceID.instanceID().token() print("InstanceID token: \(refreshedToken)") connectToFcm() } func connectToFcm() { FIRMessaging.messaging().connectWithCompletion { (error) in if (error != nil) { print("Unable to connect with FCM. \(error)") } else { print("Connected to FCM.") } } } public func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { print(userInfo) } 

我也有同样的问题,没有为我工作。 但所有你需要做的是去你的Firebase控制台,然后find你的项目,并得到它的设置,那里检查其云消息选项卡,并上传你的.p12证书。

而已! 快乐编码:)

1 – 你有没有正确地configuration你的证书在谷歌文档中指定(我不会记得在这里的过程,这是相当长的…)? ( https://firebase.google.com/docs/cloud-messaging/ios/certs#configure_an_app_id_for_push_notifications

2 – build立FCM时遇到了一些困难。 一旦我认为一切正常,但通知仍然无法正常工作,我决定彻底从手机中删除应用程序,清理我的生成文件夹,并重新安装整个事情。 之后,它正在工作。

3 – 该应用程序正在接收通知,但我仍然收到“无法获取默认令牌…”消息。 一段时间后它消失了。 不要问我为什么!

这不是一个真正的答案,我只是分享我的经验,因为我知道configuration通知不容易,每一个线索是值得欢迎的。 所以也许这个可以帮忙 干杯:)

在尝试完所有上述内容(以及其他可以find的任何内容)之后,解决问题的方法就是移动

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

在按下button时被调用,而不是在应用程序加载。

我知道这可能不是最优雅的解决scheme,但对于debugging目的来说已经足够了。 我猜测令牌不是立即可用的服务器,并需要一些时间来生成。

上面的答案涵盖了大部分的问题,但我有同样的问题,我发现以下信息有用:

  1. Firebase可以随时“旋转” (更改)用户的FCM令牌。 这是您的服务器将用于将推送通知发送到设备的128个字符的ID。

  2. Firebase文档说,最好的做法是使用委托来监视与委托callback方法的变化:

     - (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken 

[Obj – C]

 func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) 

[迅速]

每次更改都应该调用委托方法,此时可以更新服务器中的logging。

  1. 不幸的是,这不是为我工作,我有一个委托,但callback没有被调用。 所以我不得不手动更新每个应用程序启动(如上面的@micheal chein所示),如下所示的令牌:

     NSString *deviceToken = [FIRInstanceID instanceID].token; // Send this to your server 

    [对象 – C]

     let token = FIRInstanceID.instanceID().token() // Send this to your server 

    [迅速]

**重要:延迟(20-25秒)后更新标记,因为旋转有时只能反映一段时间。 你可以用这个定时器。

  1. 在此之后,我仍然得到APNS警告/错误消息:

     2017-06-06 09:21:49.520: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)" 

但是 ,推送通知每次都能正常工作。 所以我认为这个日志信息有点不合适(可能是误导)。 如果你能得到选项2为你工作,一定要做到这一点!

FCM为我工作,然后停下来。 我做了什么Rabs G.build议并删除了应用程序,并再次安装通知开始工作了。