Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does OnNewToken() actually get called?

I've looked over the documentation, and it states that OnNewToken() will get called when a new token is generated. Typically this will be on app install, restores, or if a user clears their data. There's also times, although rare, that the token will just be invalidated and a new token will be issued.

It's the last case that I'm most curious about.

If a user is using the application, and their token becomes invalid, is OnNewToken() called then, or will it just be called the next time the application starts? In my testing I haven't been able to get OnNewToken() to be called while the application is in use. I've started the application on a fresh install, retrieved the token via OnNewToken() and then deleted the user's token separately outside of that method, and OnNewToken() is not called until I stop the application and start it again.

It's fine if that's how it works. I'm just trying to figure out if I need to account for on the fly token changes while a user is running the application.

Also, a second and slightly related question, is it bad practice to check a user's FCM token when they log in to make sure they have the latest token?

like image 252
T-Rev Avatar asked Aug 09 '26 13:08

T-Rev


1 Answers

If you manager a token registry yourself, you typically check and send an updated token to the registry/server in two places:

  1. In the main method or first activity of your app by calling FirebaseMessaging.Instance.GetToken(), to ensure you have the latest token for that user when the app starts.
  2. In the onNewToken on your service, to ensure you have the latest token for that user when the service is active.

So on your second question: it is indeed quite common to call FirebaseMessaging.Instance.GetToken() in the main method or first activity.

like image 126
Frank van Puffelen Avatar answered Aug 12 '26 05:08

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!