【发布时间】:2020-10-02 19:08:39
【问题描述】:
我最近编写了一个云函数,当文档中有特定更新时,它会向特定用户发送通知,并且运行良好。但正如您在每个案例中的代码中看到的那样,我添加了代码以在案例满足时触发通知,但即使所有切换案例都失败,用户也会收到通知。我真的很困惑这个问题。
为了更好的解释: ServiceStatus有五种类型
- 类型 - 1
- 类型 - 2
- 类型 - 3
- 类型 - 4
- 类型 - 5
我希望仅在 ServiceStatus 中更新类型 - 1、3、5 时才向用户发送通知,否则应忽略函数触发器。为此,我编写了一个 switch case,但它并没有像我预期的那样工作,它会触发所有五种类型的通知,即使有两个 case 不满足。
我的代码:
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;
}
});
【问题讨论】:
-
我们看不到这个函数的输入,所以我们真的没有办法用你现在拥有的东西来调试它。您将不得不编辑问题以显示触发此功能所采取的完整步骤,包括您对文档所做的操作以使此代码运行。问题中应该有足够的信息,以便任何人都可以重现和观察行为。
-
从代码中可以看出,当一个新文档被创建时,在特定的集合中更新,然后这个函数就会被触发。因此,为了限制我编写了 switch 语句以仅在案例满足时发送通知,但即使案例不满足也会触发通知,这是我正在努力解决的错误。在枪声中,如何仅在更新特定字段时触发功能
-
您不能只指定一个字段。该函数将在任何匹配文档中发生任何更改时触发。您必须检查内容以查看发生了什么变化。 stackoverflow.com/questions/59499286/…
标签: javascript firebase google-cloud-firestore google-cloud-functions firebase-cloud-messaging