Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work

ErrorDeprecation warning or build error on FirebaseInstanceIdService

Short answer

Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes. To read the current token at any time, call FirebaseMessaging.getInstance().getToken() and listen for the result.

  • Applies to Android, Firebase Cloud Messaging Android SDK, Kotlin, Java
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every place the old classes are used.

    grep -rn -E "FirebaseInstanceId(Service)?|onTokenRefresh" app/src
  2. 2Match what you see, then jump to its fix

    1. 1The build warns or fails on FirebaseInstanceIdService, or your onTokenRefresh code never runsA service still extends FirebaseInstanceIdServiceFix
    2. 2The grep shows a FirebaseInstanceId call in an activity or repositoryCode calls FirebaseInstanceId.getInstance().getToken()Fix

What does the error look like?

  • Deprecation warning or build error on FirebaseInstanceIdService
  • Deprecation warning on FirebaseInstanceId.getInstance().getToken()

Cause 1 of 2

A service still extends FirebaseInstanceIdService

1Check

Find the class that extends FirebaseInstanceIdService and the service entry that registers it in AndroidManifest.xml.

grep -rn "FirebaseInstanceIdService" app/src

2Fix

Delete that service and its manifest entry. In your FirebaseMessagingService subclass, override onNewToken and send the token to your server. Register that service with the MESSAGING_EVENT action.

class MyMessagingService : FirebaseMessagingService() {
    override fun onNewToken(token: String) {
        sendRegistrationToServer(token)
    }
}
// AndroidManifest.xml
// <service android:name=".MyMessagingService" android:exported="false">
//   <intent-filter><action android:name="com.google.firebase.MESSAGING_EVENT" /></intent-filter>
// </service>

3Verify

Clear the app data and start the app. onNewToken should log a token.

adb logcat | grep -i token

Confirmed by firebase.google.com

Did this fix it?

Cause 2 of 2

Code calls FirebaseInstanceId.getInstance().getToken()

1Check

That call is the old way to read the token on demand.

grep -rn "FirebaseInstanceId.getInstance" app/src

2Fix

Use FirebaseMessaging and handle the task result; the token can fail to arrive, so check isSuccessful.

FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
    if (!task.isSuccessful) {
        Log.w("FCM", "Fetching FCM registration token failed", task.exception)
        return@addOnCompleteListener
    }
    val token = task.result
    // send token to your server
}

3Verify

Run the app and look for the logged token in Logcat.

adb logcat | grep FCM

Confirmed by firebase.google.com

Did this fix it?

How do I stop it coming back?

  • Send the token to your server from onNewToken, and fetch it again on app start.
  • Read the current Firebase Cloud Messaging Android guide when you upgrade the BoM, since token APIs are being phased toward Firebase Installation IDs.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.