【问题标题】:How to trigger a FCM from cloud function for a specific field update in firestore?如何从云功能触发 FCM 以更新 Firestore 中的特定字段?
【发布时间】:2020-10-02 19:08:39
【问题描述】:

我最近编写了一个云函数,当文档中有特定更新时,它会向特定用户发送通知,并且运行良好。但正如您在每个案例中的代码中看到的那样,我添加了代码以在案例满足时触发通知,但即使所有切换案例都失败,用户也会收到通知。我真的很困惑这个问题。

为了更好的解释: ServiceStatus有五种类型

  1. 类型 - 1
  2. 类型 - 2
  3. 类型 - 3
  4. 类型 - 4
  5. 类型 - 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


【解决方案1】:

如果您只想在状态字段被主动更改时发送消息,您需要比较该字段的值,因为它在此写入操作之前存在和在写入之后存在。

为此,请从beforeafter 快照中获取字段值:

const beforeTitle = event.before ? event.before.get('ServiceStatus') : "";
const afterTitle = event.after ? event.after.get('ServiceStatus') : "";

您会注意到,我还会检查 event.beforeevent.after 是否存在,因为您的云函数也会在文档创建(此时 event.before 将未定义)和文档删除(此时event.after 将未定义)。

现在使用这两个值,您可以检查是否刚刚为 ServiceStatus 字段指定了一个值,该值应该触发一条消息,并使用一系列 if 语句发送消息,例如

if (afterStatus == "Service Raised" && beforeStatus != "Service Raised") {
  ... send relevant message
}

【讨论】:

    猜你喜欢
    • 2020-01-09
    • 2019-02-02
    • 2020-02-08
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2021-08-18
    相关资源
    最近更新 更多