I am having trouble executing a cloud function that sends a custom notification to each user. For some reason the notification does not go out at all. I don't know if it is because of the promise not being properly handled. Following is the code:
exports.scheduledMonMorngRingValueNotification = functions.pubsub.schedule('00 20 * * MON').timeZone('Asia/Kolkata').onRun((context) => {
var db = admin.firestore();
const promises = [];
db.collection("collection_A").get().then(async snapshot => {
await snapshot.forEach(async item => {
await db.collection('collection_B').doc(item.id).get().then(bItem => {
// ... Some data crunching
payload = {
token: token,
fcm_options: {
analytics_label: 'aLabel',
},
notification: {
title: title,
body: body,
image: img
},
};
promises.push(admin.messaging().send(payload));
}).catch(reason => {
console.log('Could not fetch bItm: ' + reason);
});
});
}).catch(error => {
console.log('Could not fetch: ' + error);
});
return Promise.all(promises).then((result) => {
console.log('Promise all passed: ' + result + ' Promises: ' + promises);
}).catch((error) => {
console.log('Promise all failed: ' + error);
});
});