I am completely new to JavaScript and also Firebase, I am trying to get a push notification when the data is changed in the Firebase real-time database, I am able to get specific tree child notice but not the other.
the database structure is like
calendar-->{new data}
|
users-->Notices-->year-->{new data}
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendNotification = functions.database.ref("users/Notices/{Year}/{notification_time}/notice").onWrite((snapshot, context) => { const notification_id = context.params.notification_time; const Year = context.params.Year; var valueObject = snapshot.after.val(); console.log("year:" + Year + "|notification:" + notification_id); const payload = { notification: { title: "Notice for " + Year, body: snapshot.after.val(), sound: "default", click_action: "Notices" }, data: { title: Year, message: "recieved a notice" } }; return admin.messaging().sendToTopic("notifications", payload, options); });
The above snippet is good for getting the notices child update, but I need to get notified for the change in calendar child also.
You would have to do something like this:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref("users/Notices/{Year}/{notification_time}/notice").onWrite((snapshot, context) => {
const notification_id = context.params.notification_time;
const Year = context.params.Year;
var valueObject = snapshot.after.val();
return sendNotification(valueObject, Year)
});
exports.sendNotification2 = functions.database.ref("calendar").onWrite((snapshot, context) => {
var valueObject = snapshot.after.val();
return sendNotification(valueObject.message, valueObject.year)
});
function sendNotification(body, Year){
const payload = {
notification: {
title: "Notice for " + Year,
body: body,
sound: "default",
click_action: "Notices"
},
data: {
title: Year,
message: "recieved a notice"
}
};
return admin.messaging().sendToTopic("notifications", payload);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.