Sub topic 14
Storing and Maintaining FCM Tokens on Your Server
Assume you know basic Dart, Flutter asynchronous code, streams, and server-side HTTP APIs. This chapter focuses on keeping a server’s copy of an FCM registration token correct over time.
What an FCM token represents
An FCM registration token identifies one app installation on one device. It is not a permanent identifier for a person. A single user can have several tokens because they use multiple devices or reinstall the app.
The token can change after reinstalling the app, clearing app data, restoring a backup, or when Firebase rotates credentials. Your app must send the new value to your server whenever it changes.
Store a token with enough information to identify its owner and installation:
| Field | Purpose |
|---|---|
| `user_id` | The authenticated account that owns the installation |
| `token` | The current FCM registration token |
| `platform` | `android` or `ios` |
| `installation_id` | Your stable identifier for this app installation |
| `updated_at` | When the server last received the token |
Treat tokens as sensitive routing data. Require authentication when a client registers a token. Do not accept a request that lets one user attach a token to another user.
Registering and refreshing a token
Call token registration after the user signs in and after notification permission has been handled. Send the token to an authenticated server endpoint. The server should upsert the record, meaning it creates the record if it is missing or updates it if it already exists.
Future<void> registerMessagingToken() async {
final messaging = FirebaseMessaging.instance;
final settings = await messaging.requestPermission(
alert: true,
badge: true,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.denied) {
return;
}
final token = await messaging.getToken();
if (token != null) {
await sendTokenToServer(token);
}
messaging.onTokenRefresh.listen((newToken) async {
await sendTokenToServer(newToken);
});
}
Future<void> sendTokenToServer(String token) async {
// Send an authenticated HTTPS POST to your server.
// Include the platform and your installation identifier.
print('Registering token: $token');
}Call the registration method again when the authenticated user changes. A token must not remain associated with the previous account on a shared device.
Removing stale tokens
When your server sends a message, FCM can report that a token is no longer registered. For the HTTP v1 API, an UNREGISTERED response usually means that the token should be removed or disabled. Do not retry that token forever.
Also remove the token when the user signs out. If the app supports several accounts on one installation, explicitly disassociate the token from the old account before associating it with the new one.
Keep updated_at and periodically review old records. Age alone is not proof that a token is invalid, so prefer delivery failures as the reason for deletion. Use a soft-delete or disabled flag if you need an audit trail.
Token lifecycle
Exercise
A user signs in on a phone and a tablet. Should the server store one token or two?
Show answer
Two tokens. Each app installation has its own token, and both records should reference the same user account.