When is FirebaseInstanceId.getInstance().getToken() = null?

前端 未结 1 1433
没有蜡笔的小新
没有蜡笔的小新 2021-01-16 14:49

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, mig

1条回答
  •  一生所求
    2021-01-16 14:53

    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.

    0 讨论(0)
提交回复
热议问题