Change / update Firebase notification token or instance id with code?

What should I do to change or request a token in firebase? A unique token created by firebase based on device information.

+9
source share
4 answers

Now I got my answer after I encountered many problems related to generating new or changed tokens in firebase for push notifications.

1) Delete the old Firebase token

let instance = FIRInstanceID.instanceID() _ = FIRInstanceID.delete(instance) FIRInstanceID.instanceID().delete { (err:Error?) in if err != nil{ print(err.debugDescription); } else { print("Token Deleted"); } } 

2) Request a new Firebase token

 if let token = FIRInstanceID.instanceID().token() { print("Token \(token) fetched"); } else { print("Unable to fetch token"); } FIRMessaging.messaging().connect { (error) in if (error != nil) { print("Error connecting to FCM. \(error.debugDescription)") } else { print("Connected to FCM.") } } 

UPDATE FOR SWIFT 4 and Firebase 4.8.2 (follow two simple steps) 👇👇

1) Delete the old token

 let instance = InstanceID.instanceID() instance.deleteID { (error) in print(error.debugDescription) } 

2) Request a new token

 if let token = InstanceID.instanceID().token() { print("Token : \(token)"); } else { print("Error: unable to fetch token"); } Messaging.messaging().shouldEstablishDirectChannel = true 

The updated token can be obtained in the method MessagingDelegate didReceiveRegistrationToken and in didReceiveRegistrationToken Refresh.

 func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { print("Firebase Token : \(fcmToken)") } 
+10
source

Updated answer for Swift 4, FireBase 4.8.2, FirebaseMessaging (2.0.8)

 debugPrint("Existing Token :- \(Messaging.messaging().fcmToken!)") let instance = InstanceID.instanceID() instance.deleteID { (error) in print(error.debugDescription) } if let token = InstanceID.instanceID().token() { print("Token \(token) fetched"); } else { print("Unable to fetch token"); } Messaging.messaging().shouldEstablishDirectChannel = true 

We get this updated token in the MessagingDelegate method as well as in the Refresh Token

 func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { print("Firebase registration token: \(fcmToken)") } 
+2
source

InstanceID.instanceID().token() is currently deprecated.

You should use this:

 let instance = InstanceID.instanceID() instance.deleteID { (error) in print(error.debugDescription) } instance.instanceID { (result, error) in if let error = error { print("Error fetching remote instange ID: \(error)") } else { print("Remote instance ID token: \(String(describing: result?.token))") } } Messaging.messaging().shouldEstablishDirectChannel = true 

Then in AppDelegate:

 extension AppDelegate: MessagingDelegate { func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { //Here is your new FCM token print("Registered with FCM with token:", fcmToken) } 
+2
source

I understand that you want to change or update the firebase token.

Create the following two methods:

 func registerFirebaseToken() { if let token = InstanceID.instanceID().token() { print("FIREBASE: Token \(token) fetched") } else { print("FIREBASE: Unable to fetch token"); } Messaging.messaging().shouldEstablishDirectChannel = true } func unregisterFirebaseToken(completion: @escaping (Bool)->()) { // Delete the Firebase instance ID InstanceID.instanceID().deleteID { (error) in if error != nil{ print("FIREBASE: ", error.debugDescription); completion(false) } else { print("FIREBASE: Token Deleted"); completion(true) } } } 

To call

unregisterFirebaseToken (:)

and in closing check if true and then call

registerFirebaseToken ()

this will not be done the first time, and one of the delegate methods will be called ie

 extension AppDelegate: MessagingDelegate { func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) { registerFirebaseToken() } } 

This time

registerFirebaseToken ()

will be called again from the delegate method and you will get a new token.

0
source

Source: https://habr.com/ru/post/1011834/


All Articles