Stay Connected

PERSONAL BLOG

FirebaseInstanceIdService is deprecated – Android

Recently Google pushed got many updates in dependencies of Firebase along with JetPack aka AndroidX. In the new version of firebase messaging the firebaseInstanceIdService is deprecated. Now we don’t need to use INSTANCE_ID_EVENT, just extend MyInstanceIDListenerService with FirebaseMessagingService. That means no need to use FirebaseInstanceIdService service to get FCM token.

We can safely remove FirebaseInstanceIdService service. Now only need to @Override onNewToken() get Token in “FirebaseMessagingService“.


@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("FCM TOKEN "+s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
}

onNewToken will give you the token and onMessageReceived will received the message. Now no need to call onMessageReceived in another service. After all this, we also need to update the Manifest file as well and now only one service needs to be defined:


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

in the older version we get the token in activity by

FirebaseInstanceId.getInstance().getToken()

The above method getInstance().getToken is also deprecated. The new way to get the token in your activity like below:


FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListenr(Activity.this,new OnSuccessListener<InstanceIdResult>() {

@Override

public void onSuccess(InstanceIdResult instanceIdResult) {

String newToken = instanceIdResult.getToken();

}

});

Adnan Sattar

A self-driven productive Software Engineer who thrives in highly pressurized and challenging working environments by constantly improving skills. Eager to grasp new ideas and concepts from the environment.

Leave a Reply

Recent Comments

    Stay Connected

    Instagram Feed

    Categories

    ×
    %d bloggers like this: