仅在特定活动触发后才能启动FCM ID服务? [英] How to launch FCM ID Service only after a particular activity is triggered?
问题描述
假设我有一个 LoginActivity
,其中用户可以注册或使用现有凭证登录。除非用户登录并且启动应用程序的 MainActivity
,否则我不希望 FirebaseInstanceIdService
生成一个标记。
谢谢
您不能阻止 FirebaseInstanceIdService.onTokenRefresh()
直到用户登录。
你可以在你的用例中做什么:
- 如果用户未登录,则在
FirebaseInstanceIdService.onTokenRefresh()
- 当用户登录时检查
FirebaseInstanceId.getToken()
,如果!= null
手动调用onTokenRefresh()
(或直接使用您的逻辑)。
通过这种方式,您可以在用户登录时处理令牌,并且如果令牌不可用(或旋转),您将收到
onTokenRefresh()
事件稍后。
更新(2017年7月3日):在评论中,一位读者提醒说:
FirebaseInstanceIdService.onTokenRefresh
可以在用户登录后调用。
这个i对吧。当用户登录时,如果
onTokenRefresh()
getToken()
仍然可以返回null
/ code>之前没有被调用过。
您需要在应用程序中处理这个案例。
当
时,用户可以使用应用程序,但您无法发送推送通知。 code>终于被调用,如果用户之前登录过,则可以将该用户的令牌关联起来。onTokenRefresh()
Assume I have a
LoginActivity
where user can either register or login with existing credentials. I don't wantFirebaseInstanceIdService
to generate a token, unless user is logged in andMainActivity
of the application is launched.Thank you
解决方案You cannot block
FirebaseInstanceIdService.onTokenRefresh()
from being called until the user is logged in.What you could do in your use case is:
- In
FirebaseInstanceIdService.onTokenRefresh()
ignore the event if the user is not logged-in - When the user log-in check
FirebaseInstanceId.getToken()
and if!= null
callonTokenRefresh()
(or directly your logic) manually.
In this way you can process the token when the user is logged-in, and if the token is not available (or is rotated) you will receive the
onTokenRefresh()
event later.Update (July 3 2017): in the comments a reader reminded that
FirebaseInstanceIdService.onTokenRefresh()
could be called after the user log in.This is right. When the user log in,
getToken()
could still returnnull
ifonTokenRefresh()
has not been called earlier.You need to hadle this case in your app. Most likely the user can use the app anyway, but you cannot send a push notification until you received the token.
When
onTokenRefresh()
is finally called, if the user log in before, than you can associate the token the user.这篇关于仅在特定活动触发后才能启动FCM ID服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- In