如何向主题发送通知 [英] How to send notification to a Topic
问题描述
我想要一个代码将有关特定主题的通知从一台设备发送到多个设备,并且我想在订阅该主题的设备上显示该通知?我将使用Firestore存储数据和存储令牌,还将使用Firebase消息传递发送通知
I want a code to send notification from one device to multiple devices on a specific topic and I want to show that notification on devices who subscribe to that topic? I will use firestore to store data and store tokens and also use Firebase messaging to send notifications
推荐答案
发送消息 设备要求您调用Firebase Cloud Messaging API并指定FCM服务器密钥。顾名思义,此密钥只能在受信任的环境中使用,例如开发计算机,您控制的服务器或诸如Cloud Functions之类的环境。之所以需要这样做,是因为拥有FCM服务器密钥的任何人都可以向您应用的所有用户发送消息。
Sending a message to a device require that you call the Firebase Cloud Messaging API and specify the FCM Server Key. As its name implies, this key should only be used on a trusted environment, such as your development machine, a server that you control, or an environment like Cloud Functions. The reason this is required is that anyone who has you FCM server key can send messages to all users of your app.
最简单的入门方法是简单地运行 curl
命令或类似的命令,调用 旧版 FCM REST API 。在此处查看示例:如何在不使用Firebase控制台的情况下发送Firebase Cloud Messaging通知?要发送至主题,请确保至
值类似于 / topics / your_topic
。
The simplest way to get started is to simply run a curl
command or something like that, calling the legacy FCM REST API. See an example of that here: How can I send a Firebase Cloud Messaging notification without use the Firebase Console? To send to a topic, make sure the to
value is something like "/topics/your_topic"
.
要获得更高的生产水平,您可能需要引入服务器或使用云功能。然后,发送消息将成为一个多步骤过程,例如:
For a more production level, you'll probably want to introduce a server, or use Cloud Functions. Sending a message then becomes a multi-step process like:
- 想要发送消息的客户端将消息写入数据库,
- 此写操作将触发您的服务器或Cloud Functions,后者将验证请求(确定该用户有权发送此消息)。
- 然后,服务器端代码调用Firebase Admin API向主题发送消息。
- The client that wants to send a message, writes that messages to a database, or calls an API.
- This write operation triggers your server, or Cloud Functions, which validates the request (determining that this user is authorized to send this message).
- The server-side code then calls the Firebase Admin API to send a message to a topic.
有关此示例,请参见以下 functions-samples
存储库中的文件夹。
For one example of this, see this folder in the functions-samples
repo.
另请参见:
- 我的旧发送否具有Firebase数据库和Cloud Messaging的Android设备之间的调整博客文章
-
- 如何发送使用Firebase消息传递一对一消息
- 如何使用Firebase Cloud Messaging发送设备到设备消息?
- my old Sending notifications between Android devices with Firebase Database and Cloud Messaging blog post
- How to send Device to device notification by using FCM without using XMPP or any other script.?
- How to send one to one message using Firebase Messaging
- How to send device to device messages using Firebase Cloud Messaging?
这篇关于如何向主题发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!