如何从 Android 向主题发送 Firebase 消息 [英] How to send a firebase message to topic from Android

查看:27
本文介绍了如何从 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 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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