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


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.


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>