Correct way to retrieve token for FCM - iOS 10 Swift 3

前端 未结 15 2013
日久生厌
日久生厌 2021-01-31 02:42

i had implement Firebase with FirebaseAuth/FCM etc and did sent notification successfully through Firebase Console.

However i would need to push the notification from m

15条回答
  •  伪装坚强ぢ
    2021-01-31 02:53

    I was having the same problem, but could not figure out what was going on.

    The didRegisterForRemoteNotificationsWithDeviceToken suggested by @Sam is called (almost) every time, so it is a good work around. BUT, it is NOT called the first time you open the app with the refreshed token.

    So for this scenario you still need the:

    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        print("Refreshed Token: \(fcmToken)")
    }
    

    So if you only use the:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        if let fcmToken = InstanceID.instanceID().token() {
            print("InstanceID token: \(fcmToken)")
        }
    }
    

    You will only get the "refreshed token" the second time the user opens the app.

    I managed to force a refresh token by uninstalling the app and cleaning the Build Folder (Product > Clean Build Folder). Good for testing.

    Ideally it could all be handled at messaging:didReceiveRegistrationToken delegate method, but I was not able to make it work. Another way to get notified for changes in the FCM token is to listen NSNotification named kFIRMessagingRegistrationTokenRefreshNotification as suggested in the documentation: https://firebase.google.com/docs/cloud-messaging/ios/client

提交回复
热议问题