4

I understand that the FCM token renews itself if one of the following happens.

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

The following can be used at the App side to monitor Token renewal.

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);
}

My question is, when the app is terminated, the token expires and there is no way for the FCM server to know what the new token is for the device (if exist). So when I send a notification/data message to this device, the server fails to send it to the device as it doesn't know where to send it to (as there is no valid token). How do I make sure that in such situations I can notify the device ? I dont do a customer token generation. So the it seems to refresh the token now and then. How do I increase the validity of my token ?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
vzurd
  • 1,416
  • 2
  • 15
  • 37

1 Answers1

5

You will need to check for an error when sending the message, and pay attention to the error codes, as listed in the documentation. You should stop using the token if you get the error messaging/registration-token-not-registered.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 2
    Yes, I am not attempting to send the message if I get this error. My question is how can I make sure I get the message delivered to the device with the expired token ? A simple use case is when you want to notify the user of something , say a new offer or a deal, prompting the user to open the app. In this case, the app was terminated for a long time. How can I reach the user as the server doesn't know the correct token? – vzurd Feb 25 '19 at 20:34
  • 2
    You can't - it's expired. You will have to arrange, in your app, to send the refreshed token to your backend and use that instead. You should have that before the old token becomes invalid, if you arrange for that to happen ASAP. Best practice is to send it on every app launch, just in case. – Doug Stevenson Feb 25 '19 at 20:36
  • 1
    Agreed. If the app is not launched for a long period of time (probably because user doesn't use it frequently), then there is no opportunity from the app to refresh the token to back-end. In such cases, is there any other way from the server side to find the correct token ? – vzurd Feb 25 '19 at 21:57
  • 1
    Not that I know. Bear in mind that if the user isn't actively using your app, then probably don't want you invoking it remotely anyway. – Doug Stevenson Feb 25 '19 at 22:12
  • Agreed. My tokens expire within like 30 mins or so. Is there a way to extend the expiry? – vzurd Feb 26 '19 at 19:30
  • Not that I know of. – Doug Stevenson Feb 26 '19 at 19:55
  • 1
    I've only used with FCM for a day or so, but I am still playing with a token I got yesterday, way longer than 30 minutes ago. Am seeing some answers on here that they these tokens do not expire: https://stackoverflow.com/questions/41982619/when-does-a-fcm-token-expire – Dmitry Minkovsky Mar 31 '20 at 14:53