如何将消息发送到Android中的主题 [英] How to send message to the topic in Android

查看:212
本文介绍了如何将消息发送到Android中的主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过Firebase发送主题消息?



我只使用Firebase Console,
以及Post方式HTTP请求,
,但我不明白如何在Android中执行。



如何在Android
中编写一些代码要指定一个主题,然后发送一个消息到主题?

谢谢。

服务器密钥的HTTP调用。顾名思义,这个关键只能用在你可以信任的环境中。您不能直接从设备发送消息到其他设备,包括主题。 Firebase云消息传递文档中的这个

所以如果你想发送消息一个Android应用程序,您将不得不:


  1. 创建Android应用程序所对应的服务器端代码

  2. 具有该服务器端代码

    您会看到这个示例中的方法与博客文章中的方法非常相似:都侦听数据库写入来触发发送FCM消息。示例中的一些更改与博客文章相比:示例用 b


    How to send a topic message via Firebase ?

    I only found the way to send by using Firebase Console , as well as some ways about Post HTTP requests , but I don't understand how to do it in Android.

    How can I write some codes in Android to specify a topic which to target then send a message to the topic?

    Thanks.

    解决方案

    Sending a message to devices (so-called downstream messages) requires a HTTP call that specifies the server key. As its name implies, this key should only be used in environments you can trust. You cannot directly send a message from a device to other devices, including topics. This diagram from the Firebase Cloud Messaging documentation shows the flow:

    So if you want to send messages from an Android app, you will have to:

    1. create server-side code that the Android app talks to
    2. have that server-side code call Firebase Cloud Messaging to send messages
    3. have the Android app call your server-side code

    One way to accomplish such a flow is described in our blog post Sending notifications between Android devices with Firebase Database and Cloud Messaging. It uses the Firebase Database to communicate with a server-side script that then calls FCM to send the messages to topics. The server-side code in this post is a Node.js script, since it was the simplest approach available when I wrote it.

    But last week Firebase released Cloud Functions for Firebase. This allows you to run server-side code without managing your own infrastructure, which makes it a perfect fit for your use-case. In fact it is such a good fit that it's first in the documentation on use-cases for Cloud Functions for Firebase:

    You will see that the approach in this sample is very similar to the one in the blog post: both listen for database writes to trigger sending FCM messages. Some of the changes in the sample compared to the blog post:

    这篇关于如何将消息发送到Android中的主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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