node.js - 如何编写 Firebase Cloud Messaging 函数来发送推送通知

标签 node.js firebase push-notification dart flutter

<分区>

我正在尝试通过 FCM 从管理界面向用户发送推送通知, 我得到了用户订阅管理员主题的部分。

当我从 Firebase 控制台发送通知时它正在工作。 管理员可以将数据上传到 Firebase 实时数据库,其中包括通知文本和主题(如果缺少某些内容,请告诉我)。

如何编写在通知上传到实时数据库时触发的 Firebase 函数,并向订阅该主题的用户发送通知? 如果有人能与我分享一个例子,我会很高兴,因为我不熟悉 node.js ... 谢谢!

最佳答案

您需要的是概述的数据库触发器 here .您可以编写如下函数:

exports.sendNotification = functions.database  
            .ref('/notification/{topicId}/messages/{messageId}')
            .onCreate((snapshot, context) => {


    const topicId  = context.params.topicId;
    const messageId = context.params.messageId;
    const summaryLabel = topicId +"-"messageId; 
    const receivedOn = Date.now();

    const payload = {

        data: {
            topicId: topicId,
            messageId: messageId,
            time: receivedOn
        },

        notification: {
            title: "Notification Title",
            body: summaryLabel,
            icon: '/img/blue_map_icon.png',
            click_action: `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com`,
            sound: "default"
        }
    };

    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 2
    }; 

    return admin.messaging().sendToTopic(topicId, payload, options);


});

关于node.js - 如何编写 Firebase Cloud Messaging 函数来发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54463193/

相关文章:

android - Android 中所有应用的设备 token 是否相同?

node.js - child_process.exec 在路径中找不到 cmd

node.js - 如何创建nodejs服务器的多个实例

node.js - Puppeteer,与 Node.js 交换缓冲区

javascript - TypeError : this. state.posts.map 在将状态设置为数组时不是函数

java - 如何在节点Firebase中获得唯一的随机产品?

javascript - 为什么 firestore/AngularFire SDK 将多个 promise 打包到一个请求中?

reactjs - FCM 网络推送通知 Google Analytics

node.js - 嵌入式 Firebird 数据库

iOS7 上的 iOS 推送通知大负载