21

I am using FCM in my android app for push notifications. I have the below class to get the fcm token.

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    }
}

This is called correctly when my app is installed and I am able to get the token. However I am not sure if my app is updated(from play store) then this method will be called or not. The documentation just says that the method will be called whenever the token changes. But updating the app might not change the token.

anurag bhowmick
  • 351
  • 1
  • 3
  • 6

3 Answers3

22

As pointed out in the post by @Dharmitabhatt in the comments section, a registration token is not refereshed when an app is updated.

Whenever a token is refreshed in Android, it should call the onTokenRefresh() method:

Called when the system determines that the tokens need to be refreshed. The application should call getToken() and send the tokens to all application servers.

This will not be called very frequently, it is needed for key rotation and to handle Instance ID changes due to:

  • App deletes Instance ID
  • App is restored on a new device
  • User uninstalls/reinstall the app
  • User clears app data

The system will throttle the refresh event across all devices to avoid overloading application servers with token updates.

So you are right to say that updating the app doesn't necessarily refresh the token.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • 1
    Great answer, but what's a solution to it? – Sam Ramezanli Jun 12 '17 at 20:47
  • 1
    @SamRamezanli Hi. I'm not sure what solution are you looking for. – AL. Jun 13 '17 at 01:45
  • Hi @AL. How can we overcome this problem? I'm adding a feature in my app which requires firebase token. However, when the app gets an update, this token is not generated therefore my current users won't be able to use that feature. How am I able to force it to generate the token again? – Sam Ramezanli Jun 13 '17 at 15:13
  • 2
    Hey Sam. Do you mean that you recently just added FCM to your app? If so, you should simply just call `getToken()` in your initial activity. It should generate a token and from there, you could simply call your method to send the token to the server. Cheers! – AL. Jun 14 '17 at 02:45
  • @SamRamezanli You cannot refresh the token by force (bad practice), however, in order to identify users who would be using that new feature, you can perhaps store the app version somewhere and then later check in your Android code if the difference between current and the stored version is greater than 0. – Han Apr 12 '18 at 08:54
  • 1
    If the user updates a version of the App where the Firebase messaging was not yet integrated to a version where it is, the callback does not happen. I have confirmed by freshly installing the new version, in which case the callback is invoked. Anything am missing here ? – Dibzmania Oct 04 '18 at 02:13
  • I just put FCM to my application, but the application already running. This feature will release in next version. When i tested, everything works fine if new installation, but when i tried to upgraded from no fcm feature to exist fcm feature this going to be problem. Because, the onNewToken never called. I think i need to force call to this method. Can you help me? – Nanda Z Sep 04 '20 at 13:31
  • @NandaZ You don't need to force the method, it automatically triggers when needed. I would suggest posting a question with as much details as you can provide so other users would be able to see it as well. Cheers! – AL. Sep 08 '20 at 08:08
8

The registration token may change when:

  • The app deletes Instance ID

  • The app is restored on a new device

  • The user uninstalls/reinstall the app

  • The user clears app data.

You can find more info from this http://prntscr.com/dnzq9l

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

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
  • The firebase documentation seems updated, "The app deletes Instance ID" no longer in the list. – luke77 May 17 '21 at 06:12
2

Install/Clear Data - New Token is generated.

Update from Play Store - Token remains same.

If you want to call onTokenRefreshed() you should do it manually.

Click here to read more about it.

iamgopal
  • 634
  • 5
  • 12