android - 什么时候 FirebaseInstanceId.getInstance().getToken() = null?

标签 android firebase firebase-cloud-messaging

从何时何地检索 token ,何时可用?

是对 Firebase 服务器的同步调用吗?如果我在应用程序生命周期中过早地调用它,它可能还没有被填充吗?

最佳答案

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

token 由后台的 FCM 实例 ID 服务生成,该服务会在您的应用运行时立即启动。关于如何生成 token 的详细信息尚不清楚,但我认为设备需要良好的互联网连接才能与 FCM 服务器通信以获取 token 。

Is it a synchronous call to the Firebase server?

从技术上讲,没有。如 docs 中所述:

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

此时,如果 token 为空,您应该期望在您的 onNewToken() 中有一个触发器,然后您可以在其中调用 getToken(),它现在应该包含 token 。

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

通常可以尽快调用 getToken() - 在您应用的 MainActivity 中 - 在大多数情况下,当您的应用到达那个点时,它已经有了一个值。不过话又说回来,如果它是null,你还是应该妥善处理它。

关于android - 什么时候 FirebaseInstanceId.getInstance().getToken() = null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50092326/

相关文章:

android - onRetainCustomNonConfigurationInstance 服务泄漏

firebase - Firestore 集合查询作为 Flutter 中的流

php - 在 cURL 命令中获取 Firebase 云消息传递中的未经授权错误 401

java - 使用 POSTMAN 向所有用户发送 FCM 通知

android - 如何创建 asyncTask 以防止 networkOnMainThreadException

javascript - WebAppLocalServer 未定义(android 设备)

使用未指定索引的 Swift Firebase

android - Firebase 电话身份验证在 Activity 恢复时显示异常 : "The sms code has expired. Please re-send the verification code to try again."

firebase - FCM 订阅主题返回无效 token

android - 没有从 linkedin 获取电子邮件地址?