Hope all of you aware of this class, used to get notification token whenever firebase notification token got refreshed we get the refreshed token from this class, From following method.
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
To use this as i want to implement FCM, I extended MyClass from FirebaseInstanceIdService
But, Showing that FirebaseInstanceIdService is deprecated
Does anybody know this?, What method or class i should use instead of this to get refreshed token as this is deprecated.
I'm using : implementation 'com.google.firebase:firebase-messaging:17.1.0'
I checked the document for same there is nothing mentioned about this. : FCM SETUP DOCUMENT
UPDATE
This issue has been Fixed.
As Google deprecated the FirebaseInstanceService
,
I asked the question to find the way and i get to know that We can get the Token from FirebaseMessagingService,
As before, when i asked the Question Documents were not updated but Now Google docs updated so for more info, Refer this google doc : FirebaseMessagingService
OLD From : FirebaseInstanceService (Deprecated)
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
NEW From : FirebaseMessagingService
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("NEW_TOKEN",s);
}
Thanks.
firebaser here
Check the reference documentation for
FirebaseInstanceIdService
:Weirdly enough the JavaDoc for
FirebaseMessagingService
doesn't mention theonNewToken
method yet. It looks like not all updated documentation wasn't 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.
Yes
FirebaseInstanceIdService
is deprecatedNo need to use
FirebaseInstanceIdService
service to get FCM token You can safely removeFirebaseInstanceIdService
serviceNow we need to
@Override onNewToken
getToken
inFirebaseMessagingService
SAMPLE CODE
EDIT
how to get token in your activity
Now we need to use
getInstanceId ()
to generate tokengetInstanceId ()
Returns theID
and automatically generated token for thisFirebase
project.This generates an Instance ID if it does not exist yet, which starts periodically sending information to the Firebase backend.
Returns
InstanceIdResult
which holds theID
andtoken
.SAMPLE CODE
EDIT 2
Here is the working code for kotlin
FirebaseinstanceIdService
is deprecated. So have to use "FirebaseMessagingService"Sea the image please:
And this:
suppose to be solution of deprecated:
EDIT
FirebaseInstanceId.getInstance().getInstanceId().getResult().getToken()
can produce exception if the task is not yet completed, so the method witch Nilesh Rathod described (with.addOnSuccessListener
) is correct way to do it.Kotlin:
Short and Clean Answer
From FirebaseInstanceIdService documentation.
3 Changes:
FirebaseInstanceIdService
, as they have merged it inFirebaseMessagingService
.Use your old
FirebaseMessagingService
. and overrideonNewToken()
method like below.To get token at run-time, you can use
FirebaseInstanceId.getInstance().getInstanceId()
That's all, yes!
Kotlin allows for even simpler code than what's shown in other answers.
To get the new token whenever it's refreshed:
To get the token from anywhere at runtime: