FirebaseInstanceIdService is deprecated
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Google deprecated FirebaseInstanceIdService starting with Firebase Cloud Messaging SDK version 17.0.0 (released in 2018). The token management functionality that previously lived in FirebaseInstanceIdService has been consolidated into FirebaseMessagingService. If your Android app still extends FirebaseInstanceIdService, you need to migrate to the new pattern. This article explains why the change was made, shows the old and new code side by side, and covers the migration steps.
What FirebaseInstanceIdService Did
FirebaseInstanceIdService was responsible for one primary task: receiving a callback when the FCM registration token was created or refreshed. The registration token is a unique identifier that the FCM server uses to send push notifications to a specific app instance on a specific device.
The Old Pattern
You also had to register this service in AndroidManifest.xml:
Why It Was Deprecated
Google consolidated token management into FirebaseMessagingService for several reasons:
- Reduced complexity. Having two separate services for messaging and token management forced developers to maintain two service classes with overlapping responsibilities.
- Simpler lifecycle. One service means one set of lifecycle callbacks to understand and debug.
- Fewer manifest entries. Removing
FirebaseInstanceIdServiceeliminates one service declaration and its intent filter from the manifest. - Better alignment with iOS. The iOS Firebase SDK already handled tokens and messages in a single delegate, so unifying Android brought the APIs closer together.
The New Pattern
After migration, FirebaseMessagingService handles both message reception and token updates.
The manifest entry changes to:
Retrieving the Token on Demand
In the old API, you called FirebaseInstanceId.getInstance().getToken() to get the current token synchronously. This method is also deprecated. The replacement uses a task-based API.
In Kotlin with coroutines, the token retrieval is even cleaner:
Migration Steps
- Update your Firebase BOM or FCM dependency to version 17.0.0 or later in
build.gradle:
- Move the
onTokenRefreshlogic from yourFirebaseInstanceIdServicesubclass into theonNewTokenmethod of yourFirebaseMessagingServicesubclass. - Delete the old
FirebaseInstanceIdServiceclass and remove its<service>entry fromAndroidManifest.xml. - Replace
FirebaseInstanceId.getInstance().getToken()calls withFirebaseMessaging.getInstance().getToken()wherever you retrieve tokens on demand. - Replace
FirebaseInstanceId.getInstance().deleteInstanceId()withFirebaseMessaging.getInstance().deleteToken()if you need to invalidate the current token (for example, on user logout). - Test token refresh by uninstalling and reinstalling the app, or by calling
deleteToken()followed bygetToken(). Verify thatonNewTokenfires and your server receives the updated token.
Common Pitfalls
- Keeping both
FirebaseInstanceIdServiceandFirebaseMessagingServiceregistered in the manifest. This causes unpredictable behavior because both services can receive token events. - Calling the deprecated
FirebaseInstanceId.getInstance().getToken()on the main thread. It can block, and on newer SDK versions it returns null and logs a warning. - Not handling the case where
onNewTokenfires before the user has logged in. You should cache the token locally and send it to your server once the user authenticates. - Forgetting to remove the
com.google.firebase.INSTANCE_ID_EVENTintent filter from the manifest after deleting the old service. - Assuming
onNewTokenfires on every app launch. It only fires when the token actually changes. To get the current token at startup, callgetToken()explicitly.
Summary
Replace FirebaseInstanceIdService with FirebaseMessagingService by moving your onTokenRefresh logic into onNewToken. Use FirebaseMessaging.getInstance().getToken() instead of the deprecated FirebaseInstanceId.getInstance().getToken() for on-demand retrieval. Remove the old service class and its manifest entry. The migration is straightforward and consolidates your FCM code into a single service.
Related reading
- first and last day of the current month in swift
- first and last day of the current month in swift
- First letter capitalization for EditText
- Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?
- Fixing Xcode 9 issue iPhone is busy Preparing debugger support for iPhone
- Flatten an Array of Arrays in Swift
- Fling gesture detection on grid layout
- Flutter - Default image to Image.network when it fails
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.