1

From where and when is the token retrieved and when is it available?

Is it a synchronous call to the Firebase server? If I call it too soon in the app lifecycle, might it not have been populated yet?

AL.
  • 33,241
  • 9
  • 119
  • 257
Ian Warburton
  • 13,336
  • 19
  • 85
  • 164

1 Answers1

4

From where and when is the token retrieved and when is it available?

The token is generated by the FCM Instance ID service in the background, which starts as soon as your app runs. The details on how the token gets generated is unclear, but how I see it is that the device needs a decent connection to the internet in order for it to communicate with the FCM servers for the token.

Is it a synchronous call to the Firebase server?

Technically speaking, no. As mentioned in the docs:

FirebaseInstanceID.getToken() returns null if the token has not yet been generated.

At this time, if the token is null, you should expect a trigger in your onNewToken() where you could then call getToken() which should now contain the token.

If I call it too soon in the app lifecycle, might it not have been populated yet?

It's usually okay to call getToken() as soon as possible -- in your app's MainActivity -- in most cases, by the time your app reaches that point, it already has a value. But then again, you should still handle it properly if it is null.

AL.
  • 33,241
  • 9
  • 119
  • 257