5

Pretty much as the title suggests, if I have an Android app registered for FCM notifications and the app is in background or hasn't been launched in a while and the token changes, when is onTokenRefresh() called?

Will it wake the app to call onTokenRefresh() or will it just be called the next time the app is launched?

Thanks

AL.
  • 36,815
  • 10
  • 142
  • 281
rossco
  • 523
  • 4
  • 20

2 Answers2

6

The events when the onTokenRefresh() is triggered is already included in the FirebaseInstanceIdService documentation:

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.

And as also specified, the FirebaseInstanceIdService class extends the Service class, which can run regardless if the app is in foreground or background.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • What happens when user updates app from gcm to fcm & then doesn't open app.Will message delivered to a user or is it unreachable ? – Ronak Poriya Jan 05 '18 at 10:03
  • 1
    @ronakPoriya Migrating your app from GCM to FCM should not have any changes to the behavior when it comes to the delivery of the message. – AL. Jan 05 '18 at 10:08
  • Message delivery code is migrated to class MyFirebaseMessagingService.Since onTokenRefresh() from FirebaseInstanceIdService not called, That would not work.Have I understood correctly? – Ronak Poriya Jan 05 '18 at 10:29
  • @RonakPoriya Sorry. I wasn't able to understand your statement. I would suggest posting a question with the details you have. From there, I think the community would be able to help you better. Cheers! – AL. Jan 05 '18 at 10:38
2

Here is what onTokenRefresh() will be call.

  1. The app is installed.
  2. The token is compromised.
  3. The token is Changed.

So for you short answer is The onTokenRefresh() method will trigger regardless your app is in foreground or background.

teck wei
  • 1,375
  • 11
  • 22