首页 > 解决方案 > 如何从云功能触发 FCM 以更新 Firestore 中的特定字段?

问题描述

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

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

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

标签: javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functionsfirebase-cloud-messaging

解决方案


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

为此,请从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
}

推荐阅读