Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase InstanceID.instanceID().token() method is deprecated

I am working with swift and firebase. Previously I was using following method to get firebase token which then I was using to store into database to send notifications.

InstanceID.instanceID().token() 

Now this method is showing as deprecated since i have updated my firebase.

'token()' is deprecated: Use instanceIDWithHandler: instead. 

I don't know how to use instanceIDWithHandler i have tried following but don't know how to get token.

func instanceID(handler: @escaping InstanceIDResultHandler){      } 

Please help. Thank you in advance.

like image 554
Deepak Avatar asked Jun 20 '18 09:06

Deepak


People also ask

What can I use instead of FirebaseInstanceId?

FirebaseInstanceId is deprecated but now you can use FirebaseMessaging. getInstance().

What is Instance ID in Firebase?

Firebase Instance ID provides a unique identifier for each app instance and a mechanism to authenticate and authorize actions (example: sending FCM messages). Instance ID is stable except when: App deletes Instance ID. App is restored on a new device. User uninstalls/reinstall the app.


2 Answers

Fetching the current registration token

Registration tokens are delivered via the method messaging:didReceiveRegistrationToken:. This method is called generally once per app start with an FCM token. When this method is called, it is the ideal time to:

  • If the registration token is new, send it to your application server.
  • Subscribe the registration token to topics. This is required only for new subscriptions or for situations where the user has re-installed the app.

You can retrieve the token directly using instanceIDWithHandler:. This callback provides an InstanceIDResult, which contains the token. A non null error is provided if the InstanceID retrieval failed in any way.

You should import FirebaseInstanceID

  import FirebaseInstanceID 

objective C

on your getTokenMethod

[[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,                                                 NSError * _Nullable error) {     if (error != nil) {         NSLog(@"Error fetching remote instance ID: %@", error);     } else {         NSLog(@"Remote instance ID token: %@", result.token);     } }]; 

Swift

InstanceID.instanceID().instanceID { result, error in     if let error = error {         print("Error fetching remote instange ID: \(error)")     } else if let result = result {         print("Remote instance ID token: \(result.token)")     } } 

Update

InstanceID is now deprecated. Try

Messaging.messaging().token { token, error in    // Check for error. Otherwise do what you will with token here } 
like image 145
Anbu.Karthik Avatar answered Oct 02 '22 21:10

Anbu.Karthik


InstanceID is now deprecated. Try

Messaging.messaging().token { token, error in    // Check for error. Otherwise do what you will with token here } 

See Documentation on Fetching the current registration token

like image 39
Andrew Avatar answered Oct 02 '22 22:10

Andrew