Since firebase-messaging 25.1.0, how does an Android app register with FCM by Installation ID, and what replaces getToken and onNewToken?
answer
- a manifest flag opts in
- register() returns nothing useful
- the identifier arrives in a callback
- onRegistered(installationId) on each sync
- send with fid, not token
basics
~10 sSet the firebase_messaging_installation_id_enabled manifest flag, override onRegistered(installationId) to upload the Firebase Installation ID, and call register() when auto-init is off. These replace getToken, onNewToken and deleteToken, deprecated in firebase-messaging 25.1.0.
solid answer
~40 sfirebase-messaging 25.1.0 added registration by **Firebase Installation ID (FID)** and deprecated `getToken`, `deleteToken` and `onNewToken`. It is opt-in: I add `<meta-data android:name="firebase_messaging_installation_id_enabled" android:value="true" />`. Then I override `onRegistered(installationId)` in my `FirebaseMessagingService` and upload the FID with a timestamp each time it fires: on app start and routine syncs while auto-init is on, whenever the FID changes (restore to a new device, reinstall, cleared data), and after every successful `FirebaseMessaging.register()`. With auto-init disabled I call `register()` at startup myself. `unregister()` and `onUnregistered` replace `deleteToken`. The two APIs exclude each other: with the flag on, `getToken()` returns a failed `Task` with an `IllegalStateException`. On the server, messages target the `fid` field instead of the deprecated `token`.
code
kotlin · 26 linesimport com.google.firebase.messaging.FirebaseMessagingService
// Requires <meta-data android:name="firebase_messaging_installation_id_enabled"
// android:value="true" /> in the manifest.
class ParcelMessagingService : FirebaseMessagingService() {
// Worker thread. Fires after register(), on FID changes, and on routine syncs.
override fun onRegistered(installationId: String) {
RegistrationUploader.upload(installationId)
}
// Fires after FirebaseMessaging.unregister() succeeds.
override fun onUnregistered(installationId: String) {
RegistrationUploader.remove(installationId)
}
}
object RegistrationUploader {
fun upload(fid: String) {
// POST the FID to your server, which stores it with the current timestamp.
}
fun remove(fid: String) {
// Ask your server to forget this FID.
}
}go deeper
Know the names: the manifest flag, onRegistered for the identifier, register() to trigger it, and that getToken and onNewToken are deprecated since firebase-messaging 25.1.0.
Explain when onRegistered fires, why register() returns no value, why the two APIs fail each other's calls, and why the server keeps a timestamp per FID.
Plan the migration: clients on both APIs at once, a server accepting fid and token, the unregister-versus-auto-init trap on sign-out, and checks on the Task failures.
Set the team's date for leaving the token API, how long the server accepts both identifiers, and who owns the registration table and its staleness policy.
## What changed in firebase-messaging 25.1.0 For years an Android app identified itself to **Firebase Cloud Messaging (FCM)** with a **registration token**: `FirebaseMessaging.getInstance().token` fetched it, `onNewToken(token)` reported a new one, and the server stored it and sent to it. **firebase-messaging 25.1.0** added registration by **Firebase Installation ID (FID)**, the identifier the Firebase Installations service gives each app install, and marked `getToken`, `deleteToken` and `onNewToken` as deprecated, to be removed in a future release. The current release, 26.0.0, keeps both paths and also requires minSdk 24. The practical difference for a server is freshness. With legacy tokens the SDK does not refresh the registration on routine syncs, so the docs advised a periodic job, about monthly, to re-fetch and re-upload every token. With FIDs and auto-init on, the SDK keeps the registration fresh and hands the app the current FID on routine syncs at startup. ## Turning it on 1. Add the opt-in flag inside `<application>` in the manifest: `firebase_messaging_installation_id_enabled` with value `true`. The SDK reads it from the manifest meta-data and treats a missing flag as **false**, so upgrading the dependency alone changes nothing. 2. Override `onRegistered(installationId: String)` in your `FirebaseMessagingService` and upload the FID to your server, which stores it with a **timestamp** refreshed on every upload. 3. If auto-initialisation is disabled with `firebase_messaging_auto_init_enabled` set to false, call `FirebaseMessaging.getInstance().register()` at startup, for example in the main activity's `onCreate`. Its `Task<Void>` only reports success or failure; the FID arrives asynchronously in `onRegistered`. ## When the callbacks fire `onRegistered` is called on a worker thread: - after **every** successful `register()` call, even when the instance was already registered; - whenever the **FID changes** and the instance re-registers: the app restored on a new device, reinstalled, or its data cleared; - on app startup and **routine syncs** while auto-init is enabled. `onUnregistered(installationId)` fires after `FirebaseMessaging.unregister()` succeeds. From then on, sends to that FID fail with a 404. `unregister()` does not delete the FID itself (`FirebaseInstallations.delete()` does), and with auto-init enabled the instance registers again on the next start, so turn auto-init off if it must stay unregistered. ## The deprecated API and its replacement | Deprecated (token) | Replacement (Installation ID) | |---|---| | `getToken()` | `register()`, then `onRegistered` | | `onNewToken(token)` | `onRegistered(installationId)` | | `deleteToken()` | `unregister()`, then `onUnregistered` | | send target `token` | send target `fid` | | multicast `tokens` | multicast `fids` | The two client APIs are **mutually exclusive**. With the flag set to true, `getToken()` and `deleteToken()` return a failed `Task` whose exception is an `IllegalStateException` pointing at `register()` and `unregister()`. With the flag absent or false, `register()` and `unregister()` fail the same way. Code that mixes them during a migration fails quietly unless it checks the task. ## On the server A send names the FID in the message's `fid` field; the Admin SDK for Node.js models this as `FidMessage`, with `TokenMessage` deprecated. During the migration the `token` field also accepts FIDs, and a multicast may carry `fids` and `tokens` together, up to 500 targets, with tokens processed first. Move to `fid` once your clients register by FID, because `token` will be removed. ## Migrating an app that already uses tokens Installs of the old app version keep sending tokens for as long as they exist, so the migration has three steps: 1. **Server first.** Accept FIDs from clients and store them beside existing tokens, each with its own timestamp. Because the `token` send field accepts FIDs during the migration, the send path can keep working while both kinds of identifier are stored. 2. **Then the client.** Ship the manifest flag, an `onRegistered` that uploads, and no remaining `getToken`, `deleteToken` or `onNewToken` calls; any left behind now fail with an `IllegalStateException` instead of returning a token. Resubscribe to topics whenever the FID changes, so subscriptions follow the new identifier. 3. **Then the sends.** Once active installs report FIDs, send with `fid` and `fids`, and let unrefreshed tokens age out by timestamp. ## Pitfalls - Uploading only on first install. `onRegistered` fires repeatedly by design; every call is a fresh timestamp for staleness checks. - Forgetting the flag and wondering why `register()` fails. - Treating `register()`'s task result as the identifier. It completes with no value; the FID comes through the callback. - Calling `unregister()` on sign-out and expecting it to stick while auto-init re-registers on the next launch.
- What happens to sends after the app calls FirebaseMessaging.unregister()?`onUnregistered` fires and later sends to that FID fail with a 404. The FID itself survives; `FirebaseInstallations.delete()` removes it. With auto-init enabled, the instance registers again on the next app start, so disable auto-init if it must stay unregistered.
- Why does the server store a timestamp next to each FID?With auto-init on, `onRegistered` fires on routine syncs at app start, so each upload refreshes the timestamp. An FID whose timestamp is older than your staleness window, a month by FCM's own default, most likely belongs to an inactive device: stop targeting it and unsubscribe it from topics.
- Can a server that sends with the token field migrate gradually?Yes. During the migration the `token` field also accepts FIDs, and the Admin SDK's multicast send takes `fids` and `tokens` together, up to 500 in all, processing tokens first. Switch to the `fid` field once clients register by Installation ID, because `token` is deprecated and will be removed.
- getToken is still the way to get the push identifier after 25.1.0
- Upgrading firebase-messaging switches the app to Installation-ID registration automatically
- onRegistered fires once at first install, so one upload is enough
- register() returns the Firebase Installation ID in its Task result
- unregister() deletes the Firebase Installation ID itself
Assumes firebase-messaging 26.0.0; registration by Installation ID arrived in 25.1.0, which deprecated getToken, deleteToken and onNewToken.