捕获设备注册令牌以通过Firebase Cloud Messages发送通知的正确方法是什么? [英] What is the proper way to capture device registration tokens in order to send notifications via Firebase Cloud Messages?

查看:80
本文介绍了捕获设备注册令牌以通过Firebase Cloud Messages发送通知的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的玩具聊天应用程序中,我想设置一种方法,以便每当其他用户使用Firebase Google Cloud Messaging发送消息时向用户发送通知.在我的方法中,我打算捕获设备的注册令牌,然后再使用Google的Cloud Functions将通知发送到这些设备.另外,我的应用程序要求对用户进行身份验证.

In my toy chat application, I would like to set up a way to send notifications to users whenever other users send messages using Firebase Google Cloud Messaging. In my approach, I intend to capture the devices' registration tokens, and then later send notifications to those devices using Google’s Cloud Functions. Also, my application requires that users be authenticated.

要捕获Firebase实时数据库中的令牌,我将FirebaseInstanceIdService分为以下子类:

To capture the tokens in my Firebase Realtime Database, I have sub-classed FirebaseInstanceIdService as follows:

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
    private static final String TAG = "MyFirebaseInstanceIdService";
    private static final String FCM_REG_TOKENS = "fcm_registration_tokens";

    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "This device updated token: " + refreshedToken);
        sendRegistrationTokenToServer(refreshedToken);
    }

    private void sendRegistrationTokenToServer(final String refreshedToken) {
        DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
        dbRef.child(FCM_REG_TOKENS).push().setValue(refreshedToken);
        Log.d(TAG, "sendRegistrationTokenToServer: NEW TOKEN: " + refreshedToken );
    }
}

但是,上述服务似乎在启动应用程序后立即运行,甚至在运行SignInActivity之前也是如此.而且,在这一点上,显然我尚未捕获Firebase用户的详细信息,以将令牌存储在正确的数据库中.

However, it appears that the above service runs immediately the application is launched, even before the SignInActivity is run. And, at this point, obviously I am yet to capture the details of the Firebase user to store the token in the correct database.

我的直觉是我做错了.捕获设备注册令牌以便使用这些令牌向设备发送通知的正确方法是什么?

My gut feeling is that I am doing this incorrectly. What is the proper way to capture device registration tokens in order to send notifications to devices with those tokens?

推荐答案

FCM令牌绑定到已安装的应用程序(这就是为什么它们被称为实例ID令牌"的原因),不一定绑定到特定的Firebase身份验证用户(因为该应用程序甚至可能不需要用户登录.

FCM tokens are tied to an installed app (that's why they're called Instance ID Tokens), and not necessarily to a specific Firebase Authentication user (since the app may not even require the user to sign in).

您的代码是捕获应用程序实例令牌的正确方法.

Your code is the correct way to capture the token for the app instance.

如果您需要将令牌与用户相关联,则需要一些代码来检查这两个值.一种简单的方法是从您服务中的Firebase Auth获取当前用户:

If you have a requirement to associate the token with a user, then you'll need some code to check both values. A simple way to do this is to get the current user from Firebase Auth in your service:

@Override
public void onTokenRefresh() {
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "This device updated token: " + refreshedToken);
    sendRegistrationTokenToServer(refreshedToken);
}

private void sendRegistrationTokenToServer(final String refreshedToken) {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {        
        DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
        dbRef.child(FCM_REG_TOKENS).child(user.getUid()).setValue(refreshedToken);
        Log.d(TAG, "sendRegistrationTokenToServer: NEW TOKEN: " + refreshedToken );
    }
}

这为每个唯一的UID保留一个令牌.请注意,此处的正确数据结构取决于您的实际用例,因为同一用户可能在多个设备上使用您的应用程序,因此具有多个令牌.

This keeps a single token for each unique UID. Note that the correct data structure here depends on your actual use-case, since the same user may use your app on multiple devices and thus have multiple tokens.

这篇关于捕获设备注册令牌以通过Firebase Cloud Messages发送通知的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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