我可以使用特定令牌作为Firebase吗? [英] Can I use specific token for firebase?

查看:69
本文介绍了我可以使用特定令牌作为Firebase吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发React Native应用程序,并且使用Firebase推送通知服务.我有用户,所有用户都有自己的用户令牌,我想使用这些用户令牌发送推送通知.

I am developing a React Native app and I using Firebase push notification service. I have users and all users have their own user token, I want to use these user tokens for send push notification.

首先我想,当用户登录到应用程序时,我可以将用户令牌注册为主题,但是我也不确定.

First I thought, when user login to app I can register the user token to topic but I'm not sure about that too.

那么有什么方法可以使用自定义令牌向特定用户发送推送通知?

So is there any way to use custom token for send push notification to specific user?

推荐答案

Firebase Cloud Messaging没有用户的概念.它所知道的就是所谓的FCM令牌,它可以识别特定设备上已安装的特定应用.

Firebase Cloud Messaging doesn't have the concept of a user. All it knows it the so called FCM token, which identifies a specific installed app on a specific device.

如果您使用的是 react-native-firebase 库,则可以从使用令牌在以下位置上向该特定应用发送消息该特定设备.

If you're using the react-native-firebase library, you can learn how to get the FCM token from the documentation on saving tokens. Once you have the token, you can us the token to send a message to that specific app on that specific device.

如果要在使用和登录应用的任何设备上定位用户,则需要将这些设备的令牌与用户自己关联.在上面链接的代码示例中,操作如下:

If you want to target a user across any devices where they are using (and signed in to) your app, you'll need to relate the tokens of those devices to a user yourself. In the code example linked above that is done with:

firestore()
    .collection('users')
    .doc(userId)
    .update({
      tokens: firestore.FieldValue.arrayUnion(token),
    });

此代码将令牌保存到Cloud Firestore,但在任何数据库上都将起作用:它将FCM令牌( token )与当前用户( userId )相关联),以允许每个用户存储多个令牌.然后,当您想向用户发送消息时,您可以查找该用户的令牌,然后呼叫FCM以发送给所有这些令牌.

This code saves the token to Cloud Firestore, but it would work the same on any database: this associates the FCM token (token) with the current user (userId) in a way that allows multiple tokens to be stored per user. Then when you want to send a message to a user, you look up the tokens for that user and call FCM to send to all those tokens.

另请参阅:

这篇关于我可以使用特定令牌作为Firebase吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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