0

I am reading the documentation on this page under the heading "Retrieve the current registration token." I am confused by what it says.

The initial paragraph is very simple. It says we simply have to call...

FirebaseInstanceId.getInstance().getToken

That sounds very easy until I read the next paragraph that says I need to implement MyFirebaseInstanceIDService.java...

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService
{

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
}

So the first paragraph seems to say can make the call anyplace when I want to but the second paragraph has me wraping the call inside an onTokenRefresh().

So this begs the question WHEN does onTokenRefresh get called? With GCM I was able to choose WHEN I made the call asking for the token. With FCM it appears that we wait for onTokenRefresh to be called somewhere in time.

AL.
  • 36,815
  • 10
  • 142
  • 281
Dean Blakely
  • 3,535
  • 11
  • 51
  • 83

2 Answers2

0

from this post

The onTokenRefresh() method is going to be called whenever a new token is generated. Upon app install, it will be generated immediately (as you have found to be the case). It will also be called when the token has changed.

Just like an onEdit() function is called when an edit is made or onOpen() is called when the app is opened.

Adi Mabfalin
  • 316
  • 2
  • 3
  • 12
0

From the same linked document:

The registration token may change when:

  • The app deletes Instance ID
  • The app is restored on a new device
  • The user uninstalls/reinstall the app
  • The user clears app data.

It is strongly recommended that onTokenRefresh() is handled since there is a (rare) possibility that calling getToken() would (from the same docs):

This method returns null if the token has not yet been generated.

I think my answer here has a bit more explanation.

With all that said, calling getToken() would (most of the time) give you a token immediately, but in a case that it doesn't, onTokenRefresh() is there.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281