Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What method should I use now since FirebaseInstanceId.getInstance().getToken() is deprecated [duplicate]

I would like to know what would be the correct way to get Firebase token for sending push notification now that getToken() is deprecated.

like image 979
Bhaskar Jyoti Dutta Avatar asked Jul 01 '18 16:07

Bhaskar Jyoti Dutta


People also ask

What can I use instead of Firebaseinstanceid?

Firebase Instance ID has been replaced with FirebaseInstallations for app instance identifiers and FirebaseMessaging. getToken() for FCM registration tokens.

What is FirebaseMessagingService?

public class FirebaseMessagingService extends Service. Base class for receiving messages from Firebase Cloud Messaging. Extending this class is required to be able to handle downstream messages.


2 Answers

UPDATED ANSWER

FirebaseInstanceId is deprecated but now you can use FirebaseMessaging.getInstance().token.

For example:

FirebaseMessaging.getInstance().token.addOnSuccessListener { result ->         if(result != null){             fbToken = result             // DO your thing with your firebase token         } } 

OLD ANSWER

As documentation says :

This method was deprecated. In favour of getInstanceId().

getInstanceId() will return a Task with and InstanceIdResult. Like this:

 FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( new OnSuccessListener<InstanceIdResult>() {                                     @Override                 public void onSuccess(InstanceIdResult instanceIdResult) {                       String deviceToken = instanceIdResult.getToken();                       // Do whatever you want with your token now                       // i.e. store it on SharedPreferences or DB                       // or directly send it to server                  } }); 

Though is true that this approach will literally replace the use of FirebaseInstanceId.getInstanceId().getToken(), it does not solve the fact that FirebaseInstanceIdService is also deprecated leaving us with another question that is: where to use it? It can be used in any Activity context that it will always return the token. But what if we want to get the token only on creation and when it is rarely updated? For that you should override new method onNewToken from our old FirebaseMessagingService implementation: (Yes, "Messaging", not "InstanceId")

@Override public void onNewToken(String s) {     super.onNewToken(s);     String deviceToken = s;     // Do whatever you want with your token now     // i.e. store it on SharedPreferences or DB     // or directly send it to server  } 

This way code will remain leaner and wont even be necessary to use the first approach.

like image 104
Facundo Larrosa Avatar answered Oct 01 '22 23:10

Facundo Larrosa


FirebaseInstanceIdService was deprecated .!

just override onNewToken() in FirebaseMessagingService

public class LatestFirebaseMessagingService extends FirebaseMessagingService {  @Override public void onNewToken(String mToken) {     super.onNewToken(mToken);     Log.e("TOKEN",mToken); }  @Override public void onMessageReceived(RemoteMessage remoteMessage) {     super.onMessageReceived(remoteMessage); }}  

InAndroidManifest.xml

<service         android:name=".LatestFirebaseMessagingService"         android:stopWithTask="false">         <intent-filter>             <action android:name="com.google.firebase.MESSAGING_EVENT" />         </intent-filter> </service> 

getToken() This is also deprecated.!

if you need to get token in your activity use the below code.

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this,  new OnSuccessListener<InstanceIdResult>() {      @Override      public void onSuccess(InstanceIdResult instanceIdResult) {            String mToken = instanceIdResult.getToken();            Log.e("Token",mToken);      }  }); 
like image 38
KUSHA B K Avatar answered Oct 02 '22 01:10

KUSHA B K