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.
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 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)")
}
}