何时创建 fcm 刷新令牌 [英] When do fcm refresh tokens created

查看:25
本文介绍了何时创建 fcm 刷新令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 android 新手,现在正在寻找 firebase.在我的应用程序中可以登录多个手机号码.所以我正在做的是删除当前令牌

I'm new to android and now looking on firebase.In my app multiple mobile numbers can be logged in. So what am doing is deleting current token by

FirebaseInstanceId.getInstance().deleteInstanceId();

然后当我用新号码登录时,就会生成新的令牌.

and after that when I log in with new number, then new token gets generated.

所以我的问题是哪个事件触发了令牌再生事件.上面代码行的另一件事是我必须在线程(除了主线程)上运行才能工作

So my question in this exactly which event triggers token regeneration event. One more thing that above code line should I have to run on thread (other than MAIN THREAD) to work

推荐答案

这是我用来获取firebase令牌的服务

This is the service which I used for getting firebase token

    public class FCMInstanceIDListenerService extends FirebaseInstanceIdService {
AppSharedPreferences appSharedPreferences;
    @Override
    public void onCreate() {
        super.onCreate();
        String CurrentToken = FirebaseInstanceId.getInstance().getToken();
        if (CurrentToken!=null){
            Intent intent = new Intent("device_id");
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
            Log.d("token", "Refreshed token: " + CurrentToken);
            appSharedPreferences.putString("device_id",CurrentToken);
        }
        else {
               onTokenRefresh();
                }
       }

    public FCMInstanceIDListenerService() {

        appSharedPreferences=AppSharedPreferences.getsharedprefInstance(this);
        // prefManager = PrefManager.getInstance(this);
    }

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Intent intent = new Intent("device_id");
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        Log.d("token", "Refreshed token: " + refreshedToken);
        appSharedPreferences.putString("device_id",refreshedToken);
        // prefManager.putString(PrefrenceConstants.KEY_DEVICE_ID, refreshedToken);

    }

}

来自 开发者网站:

onTokenRefresh() 当系统确定令牌需要刷新.应用程序应该调用 getToken() 并发送所有应用服务器的令牌.

onTokenRefresh() Called when the system determines that the tokens need to be refreshed. The application should call getToken() and send the tokens to all application servers.

这个不会被频繁调用,key轮换需要用到并处理由于以下原因导致的实例 ID 更改:

This will not be called very frequently, it is needed for key rotation and to handle Instance ID changes due to:

  • 应用删除实例 ID

  • App deletes Instance ID

应用在新设备上恢复

用户卸载/重新安装应用

User uninstalls/reinstall the app

用户清除应用数据

系统将限制所有设备的刷新事件以避免使用令牌更新重载应用程序服务器.

The system will throttle the refresh event across all devices to avoid overloading application servers with token updates.

这篇关于何时创建 fcm 刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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