简体   繁体   中英

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

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

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:

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).

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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