I have a collection of orders mapped for a user. Which is in users/{userID}/orders/{orderID})
format. I need a function which onUpdate to this document sends a notification to an array of tokens saved in users/{userID}/tokens
exports.modifyUserCart = functions.firestore
.document('users/{userID}/orders/{orderID}')
.onUpdate((change, context) => {
const document = change.after.exists ? change.after.data() : null;
console.log(document.order_id)
// document.order_id . "This prints correctly"
// The tokens to be added to an array are in (users/{userID}/tokens). How
// do I get the tokens from the collection of tokens
var tokens = [] //array of tokens
var message = {
notification: {
title: 'Get an upfront discount',
body: "Clear your items in cart in the next hour to get an upfront
discount of $100"
},
token: tokens
};
admin
.messaging()
.send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
// perform desired operations ...
});