FCM FirebaseInstanceIdService is not available right now. How to fix it?

Carlos Daniel
Nov 4 · 2 min read
Image taken from https://android-developers.googleblog.com/2018/09/notifying-your-users-with-fcm.html

If you are working with FCM library, after version 17 getInstance().getToken() the way in which the token is retrieved changed as I explained and show how to deal with that here. Also, I’m sharing a small but powerful guide if you want to learn how to work easily with FCM push notifications in Android.

Now, if you are migrating to the newest version of the FCM library, com.google.firebase:firebase-messaging (currently the 20.0.0), you’ll notice that FirebaseInstanceIdService class is no longer available, then you need to get rid of it and fix how to get the token in the moment the app is installed (remember that FirebaseInstanceIdService class need to be registered in the AndroidManifest.xml file, and the class extending it had to override onTokenRefresh() method to get the token).

The Steps

Firstly, remove from your AndroidManifest.xml file the service regarding the Id Service:

<!-- Remove this service config -->
<service android:name=".firebase.MyAppFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>

Secondly, within the class extending FirebaseMessagingService, in which you are already overriding onMessageReceived() method, override the onNewToken(token: String) method (this replaces the old onTokenRefresh(), so all the logic you had there, must be put here).

MyAppFirebaseMessagingService

Finally, remove from the project the old service class which extends from the FirebaseInstanceIdService.

… And that is all folks!

In the last step, we don’t need to retrieve the token asynchronously, because it already comes as argument. Now everything related to FCM will work without any problem and all push notifications will be received both when the app is foreground or in background.

If you have any additional question, just let me know it in the comments.
If you need further info, you can visit Android’s FCM config page.

Thanks to Antonio Gutierrez

Carlos Daniel

Written by

GDE for Android & Android Developer @Webcat LLC

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade