2017-12-07 2 views
0

Ich versuche Listener für mehrere Knoten in der Firebase zu registrieren, derzeit meine mobile App nur zu einem Knoten hören, und es funktioniert gut, aber ich wollte es zu anderen Thema zu hören/Knoten, wie erreicht man das?NodeJS mehrere Themen Listener für 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); 
    }); 
+0

Hier haben Sie weitere Informationen: https://firebase.google.com/docs/cloud-messaging/android/topic-messaging – diegoveloper

Antwort

0

Wenn die Knoten, die eine ähnliche Struktur haben, aber an einem anderen Ort sind, sollten Sie den eigentlichen Code in eine Hilfsfunktion trennen:

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); 
    }); 

Sie dann diese Krawatte Hilfsfunktion an die zwei Positionen in der Datenbank:

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

Vielen Dank !! es funktioniert, und ich habe deine Antwort akzeptiert. – salam94

Verwandte Themen