When does a FCM token expire?

AndroidFirebaseFirebase Cloud-Messaging

Android Problem Overview


When do FCM tokens expire? Is it at 6 months?

Android Solutions


Solution 1 - Android

It doesn't expire though. It renews itself if one of the following happens.

According to https://firebase.google.com/docs/cloud-messaging/android/client:

  1. -The app deletes Instance ID
  2. -The app is restored on a new device
  3. -The user uninstalls/reinstall the app
  4. -The user clears app data.

> Monitor token generation > > The onTokenRefreshcallback fires whenever a new token is generated, so > calling getToken in its context ensures that you are accessing a > current, available registration token. Make sure you have added the > service to your manifest, then call getToken in the context of > onTokenRefresh, and log the value as shown:

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    sendRegistrationToServer(refreshedToken);
}

EDIT

onTokenRefresh() is now deprecated. onNewToken() should be used instead.

Solution 2 - Android

As stated in the documentation here the token doesn't expire it only changes on certain events. Whenever a new token is generated a method onTokenRefereshId is called.To implement this create a class which extends FirebaseInstanceIdService and override the onRefreshToken as follows:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(refreshedToken);
    }
}

Also do not forget to register this service in the manifests

<service
    android:name=".MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

Solution 3 - Android

Using firebase admin, you can do this:

async function isValidDeviceToken (deviceToken) {
  const {
    results: [notifResult]
  } = await firebaseAdmin.messaging().sendToDevice(
    deviceToken,
    {
      notification: {
        title: 'Device Registration',
        message: 'Your device has been registered.'
      }
    },
    {
      dryRun: true
    }
  );

  // returns true if valid, false if not.
  return !notifResult.error;
}

what it does is it will check if the provided deviceToken is valid, behid the scene, firebase admin checks if the deviceToken is registered. If it is not registered, it will return the following error:

{
  error: FirebaseMessagingError: The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages.
      at FirebaseMessagingError.FirebaseError [as constructor] (/Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/utils/error.js:44:28)
      at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/utils/error.js:90:28)
      at new FirebaseMessagingError (/Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/utils/error.js:256:16)
      at Function.FirebaseMessagingError.fromServerError (/Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/utils/error.js:289:16)
      at /Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/messaging/messaging.js:105:63
      at Array.forEach (<anonymous>)
      at mapRawResponseToDevicesResponse (/Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/messaging/messaging.js:101:26)
      at /Users/aprilmintacpineda/projects/my-app/node_modules/firebase-admin/lib/messaging/messaging.js:370:24
      at processTicksAndRejections (node:internal/process/task_queues:94:5)
      at async isValidDeviceToken (/Users/aprilmintacpineda/projects/my-app/test.js:13:7) {
    errorInfo: {
      code: 'messaging/registration-token-not-registered',
      message: 'The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages.'
    },
    codePrefix: 'messaging'
  }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSachin Gururaj AcharyaView Question on Stackoverflow
Solution 1 - Androiduser2967888View Answer on Stackoverflow
Solution 2 - Androidshashank chandakView Answer on Stackoverflow
Solution 3 - AndroidaprilmintacpinedaView Answer on Stackoverflow