问题描述
从何处、何时检索令牌以及何时可用?
From where and when is the token retrieved and when is it available?
它是对 Firebase 服务器的同步调用吗?如果我在应用生命周期中过早调用它,它可能还没有被填充吗?
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?
推荐答案
令牌由 FCM Instance ID 服务在后台生成,该服务会在您的应用运行后立即启动.关于如何生成令牌的详细信息尚不清楚,但我认为设备需要与互联网建立良好的连接才能与令牌的 FCM 服务器进行通信.
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.
是对 Firebase 服务器的同步调用吗?
从技术上讲,没有.如 docs 中所述:
Technically speaking, no. As mentioned in the docs:
FirebaseInstanceID.getToken()
如果尚未生成令牌,则返回 null.
此时,如果令牌为空,您应该期望在您的 onNewToken()
中有一个触发器,然后您可以在其中调用 getToken()
,它现在应该包含令牌.
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.
如果我在应用生命周期中过早调用它,它可能还没有被填充吗?
通常可以尽快调用 getToken()
- 在您应用的 MainActivity 中 - 在大多数情况下,当您的应用到达该点时,它已经有了一个值.但是话又说回来,如果它为null,您仍然应该正确处理它.
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.
这篇关于FirebaseInstanceId.getInstance().getToken() = null 什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!