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

查看:130
本文介绍了什么时候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实例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服务器的同步调用?

从技术上讲,没有.如文档:

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.

如果我在应用程序生命周期中调用它的时间过早,是否可能尚未填充?

在应用程序的MainActivity中尽快调用getToken()通常是可以的,在大多数情况下,到应用程序到达该点时,它已经有价值.但是话又说回来,如果它为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天全站免登陆