javascript - 如何从云功能触发 FCM 以更新 Firestore 中的特定字段?
问题描述
我最近编写了一个云功能,当文档中有特定更新时,它会向特定用户发送通知并且它工作正常。但是正如您在每个案例中的代码中看到的那样,我添加了代码以在案例满足时触发通知,但即使所有切换案例都失败,用户也会收到通知。我真的很困惑这个问题。
为了更好的解释: ServiceStatus有五种类型
- 类型 - 1
- 类型 - 2
- 类型 - 3
- 类型 - 4
- 类型 - 5
我希望仅在 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;
}
});
解决方案
如果您只想在状态字段被主动更改时发送消息,您需要比较该字段的值,因为它在此写入操作之前存在,并且在写入之后将存在。
为此,请从before
和after
快照中获取字段值:
const beforeTitle = event.before ? event.before.get('ServiceStatus') : "";
const afterTitle = event.after ? event.after.get('ServiceStatus') : "";
您会注意到我还会检查是否存在event.before
,event.after
因为您的云函数也会在文档创建(此时event.before
将未定义)和文档删除(此时event.after
将未定义)时触发。
现在使用这两个值,您可以检查该ServiceStatus
字段是否刚刚被赋予了一个值,该值应该触发一条带有一系列if
语句的消息,例如
if (afterStatus == "Service Raised" && beforeStatus != "Service Raised") {
... send relevant message
}
推荐阅读
- react-native - Expo/React Native TabView 在状态更改时重新渲染组件/选项卡
- python - 在单独的行中列出理解输出
- python-3.x - 使用flask-restplus时,如何编组一个简单列表(没有属性)的json?
- matlab - 如何编辑或剪切 Y 轴限制?
- c# - 如何以 xamarin 形式将列表保存到 sqlite 数据库中
- sql - 从 SQL Server 检索任何外部表数据时,Postgresql 外部数据包装器引发错误
- photoshop - 用于调整大小的 Photoshop 脚本(输出 = png,最近邻兼容性)?
- python - 使用 Python 和 BeautifulSoup 开始 Web Scraping - 分步教程中的错误
- java - 有没有办法使用“加入”查询从 Firebase 数据库中检索 android 中的数据?
- mongodb - 如何从过去一小时内没有记录的 Mongo 获取所有 ID