每當在實時數據庫中將Orderstatus
節點更改爲1
時,我正嘗試向Customers
發送通知。使用Cloud Functions更改Firebase節點時發送FCM?
但是,只要Orders
節點發生更改,就會發送推送通知。
我可以從快照&驗證Orderstatus
的值,只有在狀態爲1
時才發送通知。但是我想在Orderstatus
爲1
時觸發雲端功能。
如果我將路徑更改爲/S01/Orders/{pushId}/orderStatus
。然後,我不能讓customerEmail
請指導我來處理這一個最好的辦法....
CODE
var functions = require('firebase-functions');
var admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.shopperAlert = functions.database.ref('/S01/Orders/{pushId}')
.onWrite(event => {
var eventSnapshot = event.data;
var customerEmail = eventSnapshot.child("orderedBy").val();
console.log(customerEmail);
const getInstanceIdPromise = admin.database().ref('S01/Customers/' + customerEmail + '/Token/').once('value');
return Promise.all([getInstanceIdPromise]).then(results => {
const instanceId = results[0].val();
const payload = {
notification: {
title: "Hey",
body: "Your Order is on the Way!"
}
};
admin.messaging().sendToDevice(instanceId.FCM, payload)
.then(function (response) {
console.log("Successfully sent message:", response);
})
.catch(function (error) {
console.log("Error sending message:", error);
});
});
});