【发布时间】:2018-11-14 00:34:54
【问题描述】:
我在上面的 Firebase DB 中有这个结构
案例:当一个用户向另一个用户发送消息时,customers/id/chats/chatid 中的 newMessage 字段更新-true-
然后我要做的是从 messages/chatid 获取最后一条消息 通过我从客户/id/chats/chatid 那里得到的chatid
问题:我确实收到了有关客户的更新和数据并发送通知,但我需要最后一条消息,不知道该怎么做 完全没有 JavaScript 经验。 我在客户身上获得的聊天 ID 示例 _path: '/customers/m6QNo7w8X8PjnBzUv3EgQiTQUD12', _数据: {聊天:{'-LCPNG9rLzAR5OSfrclG':[对象]},
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 ""
});
【问题讨论】:
标签: javascript firebase firebase-realtime-database google-cloud-functions