如何从Android向主题发送Firebase消息 [英] How to send a firebase message to topic from Android
问题描述
我想从我的Android应用程序中向FCM主题发送消息.通过Firebase控制台发送消息效果很好,但是一旦用户执行了特定操作,我希望将消息发送给已订阅特定主题的所有其他用户.
I want to send a message to FCM topics from within my Android app. Sending the message through the Firebase console is working well, but once a user executes a particular action, I want a message to be sent to all other users who have subscribed to a particular topic.
在文档中有以下代码:
// The topic name can be optionally prefixed with "/topics/".
String topic = "highScores";
// See documentation on defining a message payload.
Message message = Message.builder()
.putData("score", "850")
.putData("time", "2:45")
.setTopic(topic)
.build();
// Send a message to the devices subscribed to the provided topic.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
我不知道Message
是哪个类的.显然不是RemoteMessage
.
I can't figure out from which class Message
is. It is obviously not RemoteMessage
.
推荐答案
无法使用Firebase Cloud Messaging将消息从一台Android设备直接安全地发送到另一台设备.您将始终需要服务器(或其他受信任的环境)来执行此操作.请参阅此文档部分,其中显示消息的发送方式和我的回答.此处:如何使用Firebase Messaging发送一对一消息.
There is no way to securely send messages directly from one Android device to another device with Firebase Cloud Messaging. You will always need a server (or otherwise trusted environment) to do that. See this docs section showing how messages are sent and my answer. here: How to send one to one message using Firebase Messaging.
您共享的代码示例正在使用Java的Admin SDK发送消息,该消息应在受信任的环境中运行.无法在您的Android应用中使用它.
The code sample you shared is using the Admin SDK for Java to send a message, which is meant to be run in a trusted environment. It can't be used in your Android app.
这篇关于如何从Android向主题发送Firebase消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!