5

My application is receiving the push notification from 2 firebase project. I am getting the tokens for each sender id by calling "getToken(String authorizedEntity, String scope)" separately.

String token1 = FirebaseInstanceId.getInstance().getToken("authorizedEntity1", "FCM");
String token2 = FirebaseInstanceId.getInstance().getToken("authorizedEntity2", "FCM");

As per the onTokenRefresh 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

As onTokenRefresh has been deprecated, I have checked the onNewToken, As per the documentation

Called when a new token for the default Firebase project is generated. This is invoked after app install when a token is first generated, and again if the token changes.

Q1. How to know which is the default Firebase project in case of multiple sender id ?

Q2. Suppose if "authorizedEntity1" is associated with the default firebase project then does it mean onNewToken will be invoked only when token1 will be changed ? or it will be also invoked when token2 will be changed? If it doesn't work for token2 then how to know that token2 need to be refreshed?

Q3. With reference of this my understanding is onTokenRefresh will be invoked whenever any of the token needs to be refreshed(not only for default project). Is this understanding correct ?

I want to send the updated token to the server whenever system determines that the token1 or token2 need to be refreshed.

Note: I am initializing the firebase in my application class as I am dealing with multiple sender ids.

AL.
  • 36,815
  • 10
  • 142
  • 281
Hey You
  • 303
  • 3
  • 16
  • Hey, did you find a solution for this? I'm having similar issues and Google's documentation is really not clear – Liuting Nov 15 '18 at 16:03

2 Answers2

2

After some test, I found out that only default project's token will be delivered to onNewToken. onNewToken will not be called when new token created for other sender ids by calling getToken.

Tokens retrieved by calling getToken API are consist of different string data than default token.

And these other sender id's tokens are not refreshed when default token changes. It look like they last until you explicitly call deleteToken API. (Token value didn't changed when I repeatedly call getToken.)

sNash
  • 903
  • 10
  • 11
  • @ sNash, I want to handle token **expiry/refresh** scenario. I understand that the default project's token will be delivered to onNewToken. I have multiple sender ids. Upon receiving the onNewToken callback, I am calling the "String getToken (String authorizedEntity, String scope)" for each sender id & sending it to the server(avoiding the delivered token on onNewToken). My understanding is **App will get onNewToken call back whenever default token or other token security has been compromised and the system determines that token needs to be refreshed.** This understanding is correct? – Hey You Sep 20 '18 at 04:00
  • @HeyYou onNewToken is only for default sender id. And default sender id means it's using google-service.json to get fcm token. So onNewToken will not be called for extra sender id's token refresh. I've never tested to retrieve default token by calling getToken method though. I'm using FirebaseInstanceId.getInstance().getInstanceId() for getting default token. And other sender id's token with FirebaseInstanceId.getInstance().getToken. – sNash Sep 20 '18 at 06:21
  • @HeyYou And token refreshing is meant to happen when one of these event occurs. * App deletes Instance ID * App is restored on a new device * User uninstalls/reinstall the app * User clears app data I would check extra sender id's token by calling getToken when onNewToken is called. – sNash Sep 20 '18 at 06:24
  • @ sNash, Based on your reply "onNewToken is only for default sender id. And default sender id means it's using google-service.json to get fcm token. So onNewToken will not be called for extra sender id's token refresh." Any idea how to handle application will know or get the callback when other sender id's token needs to be refreshed ? – Hey You Sep 20 '18 at 10:38
  • @HeyYou, as you mentioned in your comment above, I am also doing the same now: "In onNewToken callback, I am calling the "String getToken (String authorizedEntity, String scope)" for each sender " But, since documentation is unclear, I would recommend you to contact Firebase support (Step 3 [here](https://firebase.google.com/support/)) – garnet Sep 22 '18 at 15:21
  • @HeyYou, did you contact Firebase support? If so, please share the update. Thanks! – garnet Sep 26 '18 at 11:29
  • 2
    @garnet 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. – sNash Oct 01 '18 at 09:38
  • @sNash , Thanks for the update. I am not sure how app will handle this by without getting any callback because app never has knowledge about token's validity/expiry. I found no documentation which gives the idea for this kind of handling. – Hey You Oct 19 '18 at 07:54
  • @HeyYou, I guess you can check it's validity by comparing old token and new token regularly. IMO, This token refresh thing we've been talking about is gonna be a rare event. I would check it's validity every day and when onNewToken called or app start. Or just using one sender id and share api key to every sender. – sNash Oct 19 '18 at 08:27
1

Depending on @sNash comment who contaced Firebase Support, you should manage tokens for all sender ids other than the default sender id.

How? One simple solution 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.

The default sender is the one related to your Firebase project that the app is connected to and it can be found in google-services.json

Islam Assi
  • 991
  • 1
  • 13
  • 18