I am currently developing a function that will send notifications to all devices where a user is logged in whenever a new order document is added to their account.
Below is the code I have written to execute this function. My main query revolves around accessing the specific {userEmail}/{orderId} data from the document.
export const orderUserNotif = functions.firestore
.document('userAccounts/{userEmail}/orders/{orderId}')
.onCreate(async snapshot => {
const order = snapshot.data();
const querySnapshot = await db
.collection('userAccounts')
.doc("{userEmail}") //Looking to retrieve userEmail from the document address
.collection('tokens')
.get();
const tokens = querySnapshot.docs.map(snap => snap.id);
const payload: admin.messaging.MessagingPayload = {
notification: {
title: order!.title + " with ID " + '{orderId}', //Attempting to access order id at this point
body: `Your order has been delivered`,
}
};
return fcm.sendToDevice(tokens, payload);
})