【问题标题】:NodeJS multiple topic listener for FirebaseFirebase 的 NodeJS 多主题监听器
【发布时间】:2018-05-21 02:53:18
【问题描述】:

我正在尝试为 Firebase 中的多个节点注册监听器,目前,我的移动应用程序只监听一个节点,并且工作正常,但我希望它也能监听其他主题/节点,如何实现?

const functions = require('firebase-functions');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });

// imports firebase-functions module
// const functions = require('firebase-functions');
// imports firebase-admin module
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

/* Listens for new messages added to /messages/:pushId and sends a 
notification to subscribed users */
exports.pushNotification = 
functions.database.ref('/user_appointments/{userId}').onWrite( event => {
console.log('Push notification event triggered');
/* Grab the current value of what was written to the Realtime Database */
    var valueObject = event.data.key;
    if (!event.data.exists()) {return}
    console.log(valueObject); 

/* Create a notification and data payload. They contain the notification 
information, and message to be sent respectively */ 
    const payload = {
        data: {
            title: String(valueObject),
            message: String(valueObject)
         }
    };
 /* Create an options object that contains the time to live for the 
notification and the priority. */
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24 //24 hours
        };
      return admin.messaging().sendToTopic("user_appointments", payload, 
options);
     });

【问题讨论】:

标签: android node.js firebase firebase-realtime-database listener


【解决方案1】:

如果节点结构相似,但位置不同,则应将实际代码分离到辅助函数中:

function sendNotification(event => {
console.log('Push notification event triggered');
/* Grab the current value of what was written to the Realtime Database */
    var valueObject = event.data.key;
    if (!event.data.exists()) {return}
    console.log(valueObject); 

/* Create a notification and data payload. They contain the notification 
information, and message to be sent respectively */ 
    const payload = {
        data: {
            title: String(valueObject),
            message: String(valueObject)
         }
    };
 /* Create an options object that contains the time to live for the 
notification and the priority. */
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24 //24 hours
        };
      return admin.messaging().sendToTopic("user_appointments", payload, 
options);
     });

然后将此辅助函数绑定到数据库中的两个位置:

exports.pushNotification = 
functions.database.ref('/user_appointments/{userId}').onWrite( event => {
  sendNotification(event);
});
exports.pushNotification2 = 
functions.database.ref('/doctor_appointments/{doctorId}').onWrite( event => {
  sendNotification(event);
});

【讨论】:

  • 非常感谢!!它的工作,我已经接受了你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
  • 2017-10-03
相关资源
最近更新 更多