存储FCM的令牌 [英] Store token for FCM

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

问题描述

我正在尝试将令牌存储在我的云数据库中.稍后,我将在云功能中使用此令牌,以便向已添加为好友的用户发送通知.

I am trying to store my token in my cloud DB. I am going to use this token later in a cloud function in order to send a notification to a user that has been added as a friend.

但是,由于用户未经授权,因此无法推送设备令牌.在他们获得授权后,我无法保存它,因为令牌是在安装应用程序时生成的.

Pushing the device token however does not work because the user is unauthorized. I can't save it after they are authorized because the token is generated when the app is installed.

 private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
        FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference deviceToken = mFirebaseDatabase.getReference("device_token");
        deviceToken.push().setValue(token).addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                Log.i("token push","Success!");
            } else {
                Log.i("token push","Failed !");
            }
        });
    }

推荐答案

将令牌保存在SharedPreferences中,成功登录后将令牌保存到Firebase数据库中,请记住,如果您删除应用程序缓存或重新安装应用程序令牌总是不同的

Save the token in SharedPreferences, after a successful login save the token to Firebase database, keep in mind that if you remove the app cache or reinstall the app the token is always different

这篇关于存储FCM的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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