FirebaseInstanceIdService is deprecated now

Upasana | August 31, 2019 | 2 min read | 4,203 views


FirebaseInstanceIdService is deprecated now

Recently got many updates in firebase dependencies, one big change is that now FirebaseInstanceIdService is deprecated in favor of onNewToken in FirebaseMessagingService.

This class was deprecated.

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

What to do now?

  1. There is no need for FirebaseInstanceIdService to receive FCM token from firebase, instead FirebaseMessagingService will take care of providing new token now. So we can safely remove FirebaseInstanceIdService class from our codebase.

  2. Now we need to @Override onNewToken() get Token in FirebaseMessagingService. This method will be called whenever a token is refreshed by firebase server.

  3. Change the way fcm token is accessed from any activity using newer mechanism.

Thus our FirebaseMessagingService will look like below:

MyFirebaseMsgService.java
public class MyFirebaseMsgService extends FirebaseMessagingService {

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        Log.e("NEW_TOKEN", refreshedToken);
         // Get updated InstanceID token.
         // If you want to send messages to this application instance or
         // manage this apps subscriptions on the server side, send the
         // Instance ID token to your app server.

         //TODO: Send Token to Server
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        // Check if message contains a data payload.
    }
}

Also, we need to make sure that this class is properly registered in AndroidManifest.xml

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

How to get Token in your activity?

Now FirebaseInstanceId.getInstance().getToken() is also deprecated.

Old approach to get Token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("FCM", "Refreshed Device token: " + refreshedToken);

New way to get Token in Activity is this:

New way to get FCM Token in your activity
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this,  new OnSuccessListener<InstanceIdResult>() {
     @Override
     public void onSuccess(InstanceIdResult instanceIdResult) {
           String newToken = instanceIdResult.getToken();
           Log.e("newToken", newToken);

     }
 });

Hope that helps! happy coding.


Top articles in this category:
  1. Retrofit Basic Authentication in Android
  2. Service vs Intent Service in Android
  3. Retrofit OAuth2 Bearer Token Authentication OkHttp Android
  4. Firebase Cloud Messaging in Android App using Command Pattern
  5. iOS interview experience fresher
  6. iOS interview questions for 0-3 years experience
  7. Kotlin Coroutines with Retrofit

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.