FirebaseInstanceId.getInstance().getToken() = null 什么时候? [英] When is FirebaseInstanceId.getInstance().getToken() = null?

查看:31
本文介绍了FirebaseInstanceId.getInstance().getToken() = null 什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从何处、何时检索令牌以及何时可用?

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.

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

此时,如果令牌为空,您应该期望在您的 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 什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆