如何使用Firebase功能向每台设备发送通知?

时间:2017-12-03 16:37:33

标签: firebase firebase-realtime-database firebase-cloud-messaging firebase-notifications

您好......我正在制作一个将由我的家人使用的简单todo应用程序。

我使用firebase来存储" todo"",现在我存储了唯一的密钥,并且我只在其中存储todo title(itemName)

我试图在任何事情发生变化时向我的每个家庭成员发送通知......

exports.sendFollowerNotification = functions.database.ref(`/todo-list/{key}/{itemName}`).onWrite(event => {

我跟随:https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js

在该文件中,它表示我需要"获取设备通知令牌列表" ..

const getDeviceTokensPromise = admin.database().ref(`/shopping-list/${key}/notificationTokens`).once('value');

我不明白我如何访问这些令牌,或者我是否应该创建它们 - 如果我应该这样做,怎么做?

我没有使用身份验证,因为此应用是私有的,只会安装在5台设备上。因此,当您打开应用程序时,您已完整阅读&从firebase写入权限。

如何向每个家庭成员发送通知?

2 个答案:

答案 0 :(得分:0)

您需要在这五个设备中的每一个上使用Firebase Cloud Messaging SDK。因此,如果您使用的是Android,则必须按照 Set Up a Firebase Cloud Messaging Client App on Android的说明进行操作。添加SDK后,您可以Access the device registration token,然后send to the server

您正在使用的Cloud Functions示例假定您从Android应用程序将令牌写入Firebase数据库,然后Cloud Functions脚本可以从中读取它们并发送通知。

甚至有一个方便的web app included in the Cloud Functions sample你一直关注,这正是你需要在每个客户端做的事情。如果您要为五个设备构建Web客户端,则可以直接使用此示例,否则您必须将逻辑转换为您要定位的平台。

答案 1 :(得分:0)

我不确定FMC与其他Firebase产品的集成,但在FCM正常实施中,您可以选择向topics发送通知。

然后在设备端,当应用程序启动时,您使用FCM sdk订阅该主题。

通过这种方式,您可以避免处理管理设备的令牌ID。订阅该主题的所有设备都将收到该主题的消息。

Reference dox