我最近编写了一个云函数,当文档中有特定的更新并且可以正常工作时,该函数将通知发送给特定的用户。但是,正如您在每个案例内部的代码中看到的那样,我添加了代码以在案例满足时触发通知,但即使所有切换案例都失败,用户也会收到通知。对于这个问题,我真的很困惑。
一个更好的解释: ServiceStatus 有五种类型
我希望仅在ServiceStatus中更新类型-1、3、5时才将通知发送给用户,否则应忽略函数触发器。为此,我编写了一个switch案例,但是它不能正常工作,即使两个案例不能满足要求,它也会触发所有五种类型的通知。
我的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.OrderUpdates = functions.firestore.document('orders/{ServiceID}').onWrite(async (event) =>{
const service_id = event.after.get('ServiceID');
const title = event.after.get('ServiceStatus');
let body;
const fcmToken = event.after.get('FCMToken');
const technician_name = event.after.get('TechnicianName');
switch(title){
case "Service Raised":
body = "Thanks for raising a service request with us. Please wait for sometime our customer care executive will respond to your request";
var message = {
token: fcmToken,
notification: {
title: title,
body: body,
},
"android": {
"notification": {
"channel_id": "order_updates"
}
},
data: {
"Service ID": service_id,
},
}
let response = await admin.messaging().send(message);
console.log(response);
break;
case "Technician Assigned":
body = "Our Technician " + technician_name + " has been assigned to your service request. You can contact him now to proceed further";
var message = {
token: fcmToken,
notification: {
title: title,
body: body,
},
"android": {
"notification": {
"channel_id": "order_updates"
}
},
data: {
"Service ID": service_id,
},
}
let response = await admin.messaging().send(message);
console.log(response);
break;
case "Service Completed":
body = "Your Service request has been successfully completed. Please rate and review our service and help us to serve better. \n Thanks for doing business with us..!!";
var message = {
token: fcmToken,
notification: {
title: title,
body: body,
},
"android": {
"notification": {
"channel_id": "order_updates"
}
},
data: {
"Service ID": service_id,
},
}
let response = await admin.messaging().send(message);
console.log(response);
break;
}
});
答案 0 :(得分:1)
如果只想在状态字段被有效更改时发送一条消息,则需要比较该字段在写操作之前和写之后的值。
为此,请从centerFromTimeline(i){
google.maps.event.trigger(this.markers[i], 'click');
}
和before
快照中获取字段值:
after
您会注意到,我还检查了const beforeTitle = event.before ? event.before.get('ServiceStatus') : "";
const afterTitle = event.after ? event.after.get('ServiceStatus') : "";
和event.before
是否存在,因为您的Cloud Function也将在创建文档时触发(此时event.after
将是未定义的),并且文档删除(届时event.before
将是未定义的。)
现在有了这两个值,您可以检查event.after
字段是否刚被赋予一个值,该值应该触发通过一系列ServiceStatus
语句发送消息的消息,例如
if