Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch FCM ID Service only after a particular activity is triggered?

Assume I have a LoginActivity where user can either register or login with existing credentials. I don't want FirebaseInstanceIdService to generate a token, unless user is logged in and MainActivity of the application is launched.

Thank you

like image 477
Bravo Avatar asked May 30 '16 03:05

Bravo


1 Answers

You cannot block FirebaseInstanceIdService.onTokenRefresh() from being called until the user is logged in.

What you could do in your use case is:

  • In FirebaseInstanceIdService.onTokenRefresh() ignore the event if the user is not logged-in
  • When the user log-in check FirebaseInstanceId.getToken() and if != null call onTokenRefresh() (or directly your logic) manually.

In this way you can process the token when the user is logged-in, and if the token is not available (or is rotated) you will receive the onTokenRefresh() event later.

Update (July 3 2017): in the comments a reader reminded that FirebaseInstanceIdService.onTokenRefresh() could be called after the user log in.

This is right. When the user log in, getToken() could still return null if onTokenRefresh() has not been called earlier.

You need to hadle this case in your app. Most likely the user can use the app anyway, but you cannot send a push notification until you received the token.

When onTokenRefresh() is finally called, if the user log in before, than you can associate the token the user.

like image 69
Diego Giorgini Avatar answered Sep 23 '22 23:09

Diego Giorgini