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.
FirebaseInstanceId is deprecated but now you can use FirebaseMessaging. getInstance().
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.
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.
You can retrieve the token directly using
instanceIDWithHandler:
. This callback provides anInstanceIDResult
, 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 }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With