Orderstatus
노드가 실시간 데이터베이스에서 1
으로 변경 될 때마다 Customers
에게 알림을 보내려고합니다.Firebase 용 클라우드 기능을 사용하여 노드를 변경하면 FCM을 보냅니 까?
Orders
노드에 변경이있을 때마다 밀어 넣기 알림이 전송됩니다.
의 값을 & 상태가 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);
});
});
});