1
votes

I have a firebase query that gives a filtered list of documents from a collection for eg. 20 documents. I am able to send FCM onCreate of a document with payload and topic.

But now 20 documents are there and I have to use a loop to send FCM and each notification has a different payload based on the document.

I don't know how to use for loop to send notifications using the Cloud function.

Query sample:

const collectionRef = database.collection(booking); 
const query = collectionRef.where('status', '==', 'booked')
 .where('startTimeStamp', '>=' , admin.firestore.Timestamp.now()) 
2

2 Answers

2
votes

Assuming your function is async, you can do

const docs = (await query.get()).docs;
for (const doc of docs) {
  await whatYouDoForOneDoc(doc);
}
0
votes
Thanks, the answer helped me. Below is the whole cloud function which is currently running fine for me.


   exports.givenMinutesNotification = functions.pubsub.schedule('55 23 * * *').onRun(async context => {

        const collectionRef = database.collection(Notebook);
        const date = new Date(Date.now() + 120*60*1000);
        const timestamp = admin.firestore.Timestamp.fromDate(date)

   const query = collectionRef.where('status', '==', 'Booked')
                              .where('startTimeStamp', '>=' , timestamp);

        const docs = (await query.get()).docs;
        for (const doc of docs) {
           whatYouDoForOneDoc(doc);
        }
 });

 async function whatYouDoForOneDoc(doc)
 {
         let title = "xxxxx";
         const docID = doc.id;

         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let xxxxx = doc.get("xxxxx");
         let topic = 'xxxxx';

 console.log("-----------------docID<<<<<-----------"+ docID);

      var message = {
                 data:
                  {
                     "id": docID,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "xxxxx": xxxxx,
                     "status": Booked,
                     "topic": topic,
                      },
         notification: {
             title: title,
             body: xxxxx,

         },
         topic: topic,
     android: {
         notification: {
             clickAction: 'com.xxxxx.xxxxx."ActivityName"',
         },
     }

 };

      let response = await admin.messaging().send(message);
      console.log(response);
 }