How to send a firebase message to topic from Android(如何从 Android 向主题发送 Firebase 消息)
问题描述
我想从我的 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.
在文档中有这样的代码:
In the documentation there is this code:
// 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 消息传递一对一消息.
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.
您共享的代码示例是使用 Admin SDK for Java 发送消息,该消息旨在在受信任的环境中运行.它不能在您的 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 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 Android 向主题发送 Firebase 消息
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- UITextView 内容插图 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
