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
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
You cannot block
FirebaseInstanceIdService.onTokenRefresh()
from being called until the user is logged in.What you could do in your use case is:
FirebaseInstanceIdService.onTokenRefresh()
ignore the event if the user is not logged-inFirebaseInstanceId.getToken()
and if!= null
callonTokenRefresh()
(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 returnnull
ifonTokenRefresh()
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.I am maintaining one flag in shared pref which indicates whether gcm token sent to server or not. In Splash screen every time I am calling one method sendDevicetokenToServer. This method checks if user id is not empty and gcm send status then send token to server.
In MyFirebaseInstanceIDService class
Sorry but that is not possible.
FirebaseInstanceIdService
call automatically on application startup and generate aToken
. Keep in mind that its related with applicationInstance
. Not with the one particular user. If you are trying to saveToken
with one particular user (i.e when user logged in then you will save thattoken
in server db for push notification of that user).If you are doing this a bug you will faced in future is that if Two user share one applicationInstance
then push notification may be pushed to wrong user .. Hope you get my point.