Firebase subscribeToTopic孩子

时间:2017-11-28 02:55:05

标签: javascript android node.js firebase firebase-cloud-messaging

目前我正在尝试为我的移动应用实施设备到设备通知。我能够实现这一点,但是,我希望听取节点user_appointments的子节点而不仅仅是user_appointment。示例:user_appointment / any_user_id,用户ID不固定。

这是我目前的代码:

(节点JS)

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

    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.val();
       /* Create a notification and data payload. They contain the 
       notification information, and message to be sent respectively */ 
       const payload = {
       notification: {
          title: 'Semuka',
          body: "Your appointment list has been updated. Tap to view.",
          sound: "default"
       },
       data: {
          title: "title",
          message: "message"
       }
    };
    /* 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应用程序)

    FirebaseMessaging.getInstance()
   .subscribeToTopic(user_appointments);

0 个答案:

没有答案