I am sending push notification by the following way:
exports.sendPushNotification =
functions.database.ref('/chat_rooms/{id}').onWrite(event => {
console.log(event);
const original = event.data.val();
const reciverId = original['receiverID'];
const text = original['text'];
const senderName = original['senderName'];
var path = 'fcmToken/' + reciverId;
const payload = {
notification: {
title :senderName,
body: text,
badge:'7',
sound:'default',
}
};
return admin.database().ref(path).once('value').then(allToken => {
if (allToken.val()){
var firebaseReceiverID = allToken.val();
var firebaseToken = firebaseReceiverID['firebaseToken'];
return admin.messaging().sendToDevice(firebaseToken,payload).then(response => {
});
};
});
});
My firebase database structure is like this:
how to send push if any child is added under chatroom(to detect the path)