Reputation: 151
I have two types of questions around FirebaseInstanceId.getToken(String authorizedEntity, String scope)
, one around calling this method multiple times and one around whether calling this method triggers FirebaseMessagingService.onNewToken(String token)
.
1) Calling multiple times:
According to this documentation one would call getToken(String authorizedEntity, String scope)
multiple times, each time with a different sender id, in order to be able to receive messages from multiple senders. My question is, will each call return a different token, or will each call return the same token but now the token will work also for multiple senders? If we call this method with a sender id that we've previously used before, will that return the existing token or generate a new one?
So, say I have this order of operation
getToken("senderId1", "FCM")
and get token A
getToken("senderId2", "FCM")
. Will I get A
or a different token B
?getToken("senderId2", "FCM")
. Will I get A
, B
, or yet another different one C
?2) Will onNewToken
be called?
This documentation states that the method will be invoked if the token changes. So does this mean that if getToken
returns a different token than before then onNewToken
will be invoked as well? If we're going to be calling getToken
multiple times to allow for receiving from different senders, and each call returns a different token, then onNewToken
will keep getting invoked.
Since it is advised that we update our server when onNewToken
is triggered, I want to understand the expected behavior and avoid generally updating the server on each invocation of onNewToken
.
Upvotes: 5
Views: 2167
Reputation: 1037
The token is unique for every sender id. Different sender ids have different tokens.
Default sender is the one defined in your google-services.json (related to the firebase project that the app is connected to)
OnNewToken is called when token for the default sender is changed. There are no callbacks triggered when a token for a specific sender (other than default) is changed.
As mentioned by @sNash who contacted firebase support:
I contacted firebase support team and got an answer. Answer summary : Different sender id's token will not be automatically managed by firebase cloud messaging. So developer is responsible for it's management. You guys have to check it's validity manually with your own method.
How to determine if token needs to be refreshed in case of multiple sender id?
Managing tokens for specific sender ids (other than default):
One simple solution for managing tokens for specific sender ids (other than default) is through storing all sender ids with their tokens in SharedPreferences or in db. When app starts, check if the token changed for each sender by comparing the stored token with the token returned by
FirebaseInstanceId.getInstance().getToken(SENDER_ID, "FCM");
Moreover, do the same check in onNewToken method. There is a chance that tokens other than the default may be changed when the default token is changed.
Upvotes: 0
Reputation: 37778
My question is, will each call return a different token, or will each call return the same token but now the token will work also for multiple senders?
getToken()
/ getToken(String, String)
will return the same token until such time that the corresponding token expires. Note that by same token, I mean the same token that they return for each sender. i.e.:
getToken()
returns the registration token for the default project (e.g. tokenDefaultSenderId)getToken(String, String)
returns the registration token for the sender it is associated to (e.g. tokenSenderId2)If we call this method with a sender id that we've previously used before, will that return the existing token or generate a new one?
B
.B
again.A token is tied to the sender it is associated to.
Will onNewToken be called? ... So does this mean that if getToken returns a different token than before then onNewToken will be invoked as well?
onNewToken()
will only return the token for the default sender (emphasis mine):
Called when a new token for the default Firebase project is generated.
The thing about onNewToken()
is that it triggers only when the previous token has expired -- the thing to ask is, if the token for the default sender expires, what more for the other senders? So the best workaround here is to call getToken()
for each of the sender that you have, like so:
public void onNewToken(String token){
String default = token;
String sender2 = getToken("senderId2", "FCM");
// and so on for each of your sender, then save the tokens as needed
}
Upvotes: 4