FirebaseInstanceIdService is deprecated now

Munish Chandel | July 28, 2018 at 02:13 PM | 261 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:
public class MyFirebaseMsgService extends FirebaseMessagingService {

    public void onNewToken(String s) {
         // 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

    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Not getting messages here? See why this may be:
        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

            <action android:name="" />

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>() {
     public void onSuccess(InstanceIdResult instanceIdResult) {
           String newToken = instanceIdResult.getToken();
           Log.e("newToken", newToken);


Hope that helps! happy coding.

Buy DRM Free PDF for Complete Collection of Interview Questions
Generic placeholder image
ebook PDF - Cracking Spring Microservices Interviews for Java Developers

This ebook discusses 120 plus real problems and their solutions for Spring microservices architecture based on Spring Boot, Spring Cloud, Cloud Native Applications. It covers core concepts of microservices architecture, various design patterns, interview questions & answers, security in microservices, testing strategies and best practices in distributed system design.

Free Email Updates
Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Similar Articles:
  1. Retrofit Basic Authentication in Android App with OkHttp
  2. FirebaseInstanceIdService is deprecated now
  3. Firebase Cloud Messaging in Android App using Command Pattern
Facebook Page
Related Topics
Free Email Updates (100+ Subscribers)

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

This website uses cookies to ensure you get the best experience on our website. more info