简体   繁体   English

在云函数 FCM 中向特定用户发送推送通知

[英]Sending push notification to a specific user in cloud function FCM

Please note that this question is not about getting device token on client side.请注意,此问题与在客户端获取设备令牌无关。

I am very new to react-native and firebase and I want to implement push notication service of FCM.我对react-nativefirebasefirebase ,我想实现 FCM 的推送通知服务。 What I am planning to do is to send a notification to particular device using its device token on my nodejs app server or cloud function( not through console) .我打算做的是在我的 nodejs 应用程序服务器或云功能(不是通过控制台)上使用其设备令牌向特定设备发送通知。 But as I have found, FCM doesn't provides any API to accrss token by username.但正如我发现的那样,FCM 没有提供任何 API 来通过用户名访问令牌。 Suppose I want to send notification to user X( for the time being, suppose that one user signs only on single device).假设我想向用户 X 发送通知(暂时假设一个用户仅在一台设备上签名)。 Now using the function sendToDevice() , I can send the message to a specific device.现在使用函数sendToDevice() ,我可以将消息发送到特定设备。 But how would I know the device token of user X. Do I need to store tokens in the firebase database by myself?但是我怎么知道用户 X 的设备令牌。我需要自己在 firebase 数据库中存储令牌吗? Or can I get along without storing FCM tokens?或者我可以在不存储 FCM 令牌的情况下相处吗?

Please guide me because firebase docs aren't clear about this.请指导我,因为 firebase 文档对此不清楚。

Save the device token for a particular user in DB and then you can use fcm-node npm in which there is a simple function to send a push to a particular device token.将特定用户的设备令牌保存在 DB 中,然后您可以使用 fcm-node npm,其中有一个简单的函数可以向特定设备令牌发送推送。

You also need to use the FCM server key which you will get easily on the firebase console.您还需要使用 FCM 服务器密钥,您可以在 Firebase 控制台上轻松获取该密钥。

FCM doesn't offer a way to associate device tokens to individual users who might be using your app from multiple devices. FCM 不提供将设备令牌与可能从多个设备使用您的应用程序的单个用户相关联的方法。 You will need to write code to associate a device token to a user account by sending that token to your backend, along with the user's ID.您需要编写代码来将设备令牌与用户帐户相关联,方法是将该令牌与用户 ID 一起发送到您的后端。 Only then can you collect FCM tokens for that user, and message them on any of their devices.只有这样,您才能为该用户收集 FCM 令牌,并在他们的任何设备上向其发送消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM