Reputation: 1090
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.
Upvotes: 53
Views: 44597
Reputation: 956
Update for Objective-C
[[FIRMessaging messaging] tokenWithCompletion:^(NSString * _Nullable token, NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Error fetching remote instance ID: %@", error);
} else {
NSLog(@"Remote instance ID token: %@", token);
}
}];
Upvotes: 0
Reputation: 175
Here the solution
The problem is that FirebaseInstanceID has recently become deprecated
Old
InstanceID.instanceID().instanceID {result, _ in
New
Messaging.messaging().token { (token, _) in
You can replace it like above, it working
Check below link
Upvotes: 9
Reputation: 82769
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
}
Upvotes: 68
Reputation: 719
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
Upvotes: 58