What Method Should I Use Now Since Firebaseinstanceid.Getinstance().Gettoken() Is Deprecated

Firebase instance id: deprecation of getId() in 21.0.0

FirebaseInstanceId class is deprecated, to get token use FirebaseMessagingClass. The token can be generated using the following code:

FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "Fetching FCM registration token failed", task.getException());
return;
}

// Get new FCM registration token
String token = task.getResult();

// Log and toast
String msg = getString(R.string.msg_token_fmt, token);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});

Regarding the Firebase InstanceId, this is what the official document says:

public Task getInstanceId () ->
This method is deprecated.
For an instance identifier, use FirebaseInstallations.getId() instead. For an FCM registration token, use FirebaseMessaging.getToken() instead.

FirebaseInstanceId.getInstance().getToken() deprecated how to save token in DB

You should save the token from within the onSuccess callback of the getInstanceId() call:

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(MainActivity.this, new OnSuccessListener<InstanceIdResult>(){
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String deviceToken = instanceIdResult.getToken();
Log.e("newToken", deviceToken);

... save the token to your database here
}
});

FirebaseInstanceIdService is deprecated

firebaser here

Check the reference documentation for FirebaseInstanceIdService:

This class was deprecated.

In favour of overriding onNewToken in FirebaseMessagingService. Once that has been implemented, this service can be safely removed.

Weirdly enough the JavaDoc for FirebaseMessagingService doesn't mention the onNewToken method yet. It looks like not all updated documentation has been published yet. I've filed an internal issue to get the updates to the reference docs published, and to get the samples in the guide updated too.

In the meantime both the old/deprecated calls, and the new ones should work. If you're having trouble with either, post the code and I'll have a look.

FirebaseInstaceId, InstanceIdResult deprecated

That's not how you should get the token. All those methods are deprecated. Nowadays, the solution is:

FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (task.isSuccessful()) {
String token = task.getResult();
//Do what you need to do with your token
Log.d(TAG, token);
}
}
});

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

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
}


Related Topics



Leave a reply



Submit