How to launch FCM ID Service only after a particular activity is triggered?(如何仅在触发特定活动后启动 FCM ID 服务?)
问题描述
假设我有一个 LoginActivity,用户可以在其中注册或使用现有凭据登录.我不希望 FirebaseInstanceIdService 生成令牌,除非用户已登录并且应用程序的 MainActivity 已启动.
谢谢
在用户登录之前,您不能阻止调用 FirebaseInstanceIdService.onTokenRefresh().
你可以在你的用例中做的是:
- 在
FirebaseInstanceIdService.onTokenRefresh()如果用户未登录则忽略该事件 - 当用户登录检查
FirebaseInstanceId.getToken()并且如果!= null调用onTokenRefresh()(或直接你的逻辑) 手动.
这样你就可以在用户登录的时候处理token,如果token不可用(或者被轮换)你会在稍后收到onTokenRefresh()事件.p>
更新(2017 年 7 月 3 日):有读者在评论中提醒用户登录后可以调用 FirebaseInstanceIdService.onTokenRefresh().
这是对的.当用户登录时,如果之前没有调用 onTokenRefresh(),getToken() 仍然可以返回 null.
您需要在您的应用中处理这种情况.用户很可能仍然可以使用该应用程序,但在收到令牌之前您无法发送推送通知.
当onTokenRefresh()最终被调用时,如果用户之前登录过,则可以将token关联到用户.
Assume I have a LoginActivity where user can either register or login with existing credentials. I don't want FirebaseInstanceIdService to generate a token, unless user is logged in and MainActivity 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!= nullcallonTokenRefresh()(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 return null if onTokenRefresh() 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 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何仅在触发特定活动后启动 FCM ID 服务?
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
