Home > Net >  How to update FCM token
How to update FCM token

Time:12-23

I am having the same issue where the key gets generated 2 times.

As stated here: https://github.com/firebase/firebase-ios-sdk/issues/6813

My question is how I can always keep my token updated with the Messaging didReceiveRegistrationToken: callback?

extension AppDelegate: MessagingDelegate {
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {

      let deviceToken:[String: String] = ["token": fcmToken ?? ""]
        print("Device token: ", deviceToken) 

    }
}

CodePudding user response:

The last comment from a contributor on github says:

This is expected. We recently discover that sometimes, the APNS token is collected from Apple with a slight delay after first token request was issued, so client has to issue a second token request with the apns token mapping in it. So you are getting that scenario which is why you are getting the token callback twice. It wasn't happening before because there was a bug that the second token request is not triggered and was recently fixed in https://github.com/firebase/firebase-ios-sdk/pull/6669. This should not affect your work as long as you always keep your token updated with the Messaging didReceiveRegistrationToken: callback."(charlotteliang)

So you should be fine as long as you update the token on every didReceiveRegistrationToken call. You can do that as the documentation describes: https://firebase.google.com/docs/cloud-messaging/ios/client#monitor-token-refresh.

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
  print("Firebase registration token: \(String(describing: fcmToken))")

  let dataDict: [String: String] = ["token": fcmToken ?? ""]
  NotificationCenter.default.post(
    name: Notification.Name("FCMToken"),
    object: nil,
    userInfo: dataDict
  )
  // TODO: If necessary send token to application server.
  // Note: This callback is fired at each app startup and whenever a new token is generated.
}

  • Related