Firebase FCM 强制调用 onTokenRefresh() [英] Firebase FCM force onTokenRefresh() to be called

查看:10
本文介绍了Firebase FCM 强制调用 onTokenRefresh()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用从 GCM 迁移到 FCM.

I am migrating my app from GCM to FCM.

当新用户安装我的应用时,会自动调用 onTokenRefresh().问题是用户还没有登录(没有用户ID).

When a new user installs my app, the onTokenRefresh() is automatically being called. The problem is that the user is not logged in yet (No user id).

如何在用户登录后触发onTokenRefresh()?

How can I trigger the onTokenRefresh() after the user is logged-in?

推荐答案

onTokenRefresh() 方法将在生成新令牌时调用.应用安装后,它将立即生成(正如您所发现的那样).当令牌改变时它也会被调用.

The onTokenRefresh() method is going to be called whenever a new token is generated. Upon app install, it will be generated immediately (as you have found to be the case). It will also be called when the token has changed.

根据 FirebaseCloudMessaging 指南:

您可以将通知定位到单个特定设备.在初始启动您的应用程序时,FCM SDK 会生成一个注册令牌,用于客户端应用实例.

You can target notifications to a single, specific device. On initial startup of your app, the FCM SDK generates a registration token for the client app instance.

来源链接:https://firebase.google.com/docs/notifications/android/console-device#access_the_registration_token

这意味着令牌注册是针对每个应用的.听起来您想在用户登录后使用令牌.我建议您将 onTokenRefresh() 方法中的令牌保存到内部存储或共享首选项.然后,在用户登录后从存储中检索令牌并根据需要将令牌注册到您的服务器.

This means that the token registration is per app. It sounds like you would like to utilize the token after a user is logged in. What I would suggest is that you save the token in the onTokenRefresh() method to internal storage or shared preferences. Then, retrieve the token from storage after a user logs in and register the token with your server as needed.

如果您想手动强制onTokenRefresh(),您可以创建一个 IntentService 并删除令牌实例.然后,当您调用 getToken 时,将再次调用 onTokenRefresh() 方法.

If you would like to manually force the onTokenRefresh(), you can create an IntentService and delete the token instance. Then, when you call getToken, the onTokenRefresh() method will be called again.

示例代码:

public class DeleteTokenService extends IntentService
{
    public static final String TAG = DeleteTokenService.class.getSimpleName();

    public DeleteTokenService()
    {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        try
        {
            // Check for current token
            String originalToken = getTokenFromPrefs();
            Log.d(TAG, "Token before deletion: " + originalToken);

            // Resets Instance ID and revokes all tokens.
            FirebaseInstanceId.getInstance().deleteInstanceId();

            // Clear current saved token
            saveTokenToPrefs("");

            // Check for success of empty token
            String tokenCheck = getTokenFromPrefs();
            Log.d(TAG, "Token deleted. Proof: " + tokenCheck);

            // Now manually call onTokenRefresh()
            Log.d(TAG, "Getting new token");
            FirebaseInstanceId.getInstance().getToken();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private void saveTokenToPrefs(String _token)
    {
        // Access Shared Preferences
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();

        // Save to SharedPreferences
        editor.putString("registration_id", _token);
        editor.apply();
    }

    private String getTokenFromPrefs()
    {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        return preferences.getString("registration_id", null);
    }
}

编辑

FirebaseInstanceIdService

公共类 FirebaseInstanceIdService 扩展服务

public class FirebaseInstanceIdService extends Service

该类已弃用.赞成覆盖FirebaseMessagingService 中的 onNewToken.一旦那已经实施后,可以安全地删除此服务.

This class is deprecated. In favour of overriding onNewToken in FirebaseMessagingService. Once that has been implemented, this service can be safely removed.

onTokenRefresh() 已弃用.在 MyFirebaseMessagingService

onTokenRefresh() is deprecated. Use onNewToken() in MyFirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.e("NEW_TOKEN",s);
    }

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    }
} 

这篇关于Firebase FCM 强制调用 onTokenRefresh()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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