I have this structure in Firebase DB above
Case: When a user sends a message to another user, newMessage field is updated -true- in customers/id/chats/chatid
Then what I am trying to do is fetching last message from messages/chatid via the chatid I am getting from customers/id/chats/chatid
Problem: I do get the update and data on customers and sending notification but I need that last message, Dont know how to do that No JavaScript experience at all. Sample Chat id which I get on customers _path: '/customers/m6QNo7w8X8PjnBzUv3EgQiTQUD12', _data: { chats: { '-LCPNG9rLzAR5OSfrclG': [Object] },
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotif = functions.database.ref('/customers/{id}/chats/{id}/').onUpdate((event) => {
const user = event.data.val();
console.log('Event data: ', event.data);
//HERE I WANT TO USE THAT CHAT ID TO FETCH MESSAGE in MESSAGES.
// Get last message and send notification.
// This works when newMessage field is updated.
// However I neeed the message content from another table.
var myoptions = {
priority: "high",
timeToLive: 60 * 60 * 24
};
// Notification data which supposed to be filled via last message.
const notifData = {
"notification":
{
"body" : "Great Match!",
"title" : "Portugal vs. Denmark",
"sound": "default"
}
}
admin.messaging().sendToDevice(user.fcm.token, notifData, myoptions)
.then(function(response) {
console.log('Successfully sent message:', response);
})
.catch(function(error) {
console.log('Error sending message:', error);
});
return ""
});
