简体   繁体   English

如何从云功能触发 FCM 以进行 Firestore 中的特定字段更新?

[英]How to trigger a FCM from cloud function for a specific field update in firestore?

I recently wrote a cloud function that sends a notification to a specific user when there is a specific update in a document and it works fine.我最近编写了一个云函数,当文档中有特定更新时,它会向特定用户发送通知,并且工作正常。 But as you can see in my code inside each case I added the code to trigger a notification whenever the case satisfies but the user receives notification even all the switch cases fail.但是正如您在每个案例中的代码中看到的那样,我添加了代码以在案例满足时触发通知,但即使所有切换案例失败,用户也会收到通知。 I'm really confused about this issue.我真的很困惑这个问题。

For a better explanation: ServiceStatus are of five types更好的解释: ServiceStatus有五种类型

  1. Type - 1类型 - 1
  2. Type - 2类型 - 2
  3. Type - 3类型 - 3
  4. Type - 4类型 - 4
  5. Type - 5类型 - 5

I want the notification to be sent to the user only when the type - 1, 3, 5 are updated in the ServiceStatus else the function trigger should be ignored.我希望只有在 ServiceStatus 中更新类型 - 1、3、5 时才将通知发送给用户,否则函数触发器应该被忽略。 For this, I wrote a switch case but that is not working as I expected it triggers a notification for all five types even though two cases won't satisfy.为此,我编写了一个 switch case,但它并没有像我预期的那样工作,即使两种情况都不满足,它也会触发所有五种类型的通知。

my code:我的代码:

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;
    }
});

If you only want to send a message when the status field is actively changed, you'll want to compare the value of that field as it existed before this write operation and as it'll exist after the write.如果您只想在状态字段被主动更改时发送消息,您需要比较该字段的值,因为它在此写入操作之前存在和在写入之后存在。

To do this, get the field value from the before and after snapshots:要做到这一点,您可以通过字段值beforeafter的快照:

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

You'll note that I also check if event.before and event.after exist, as your Cloud Function will also be triggered upon document creation (at which point event.before will be undefined) and document deletion (at which point event.after will be undefined).您会注意到,我还会检查event.beforeevent.after存在,因为您的云功能也将在文档创建(此时event.before将未定义)和文档删除(此时event.after未定义)时event.after将是未定义的)。

Now with those two values you can check whether the ServiceStatus field was just given a value that should trigger a message to be sent with a series of if statements, like现在有了这两个值,您可以检查ServiceStatus字段是否刚刚被赋予了一个值,该值应该触发一条消息以一系列if语句发送,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM