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

When is FirebaseInstanceId.getInstance().getToken() = null?(FirebaseInstanceId.getInstance().getToken() = null 什么时候?)
本文介绍了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 什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)