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
The onTokenRefresh() method is going to be called whenever a new token is generated. Upon app install, it will be generated immediately (as you have found to be the case). It will also be called when the token has changed.
FirebaseInstanceIdService performs security checks at runtime, no need for explicit permissions despite exported="true" --> <service android:name=".MyFirebaseInstanceIdService" android:exported="true"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With