Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is FirebaseInstanceIdService onTokenRefresh() only called when the app is running?

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

like image 915
rossco Avatar asked Jan 11 '17 03:01

rossco


People also ask

What is onTokenRefresh?

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.

What is FirebaseInstanceIdService?

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>


1 Answers

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.

like image 67
AL. Avatar answered Nov 15 '22 06:11

AL.