I'm successfully using a Firebase Cloud Function in Typescript to send a notification to users when a document in a Firestore collection is created.
However, because I store the same document at two different locations, it's sending the notifications out twice. I want the notification to be sent only once.
Is there a way to use another cloud function to get, say, the current user, and then use that in the query for my function that sends notification so it only looks for writes / reads in changes that happen for that specific user only?
Here is my code in Cloud Functions:
export const notificationOnMessageReceived =
functions.firestore
.document('users/{uid}/matches/{match}/messages/{message}') //HOW CAN I GET UID FROM ANOTHER FUNCTION AND USE IT HERE TO SPECIFY EXACTLY THE USER FOR WHICH IT SHOULD RUN?
.onCreate(async snapshot => {
const message = snapshot.get('Message');
const token = snapshot.get('Device token receiver');
console.log('token: ' + token);
const payload = {
notification: {
title: `New message`,
body: `${message}`
}
};
return fcm.sendToDevice(token, payload);
});
uid
of the user who created the document underusers/{uid}/matches/{match}/messages/{message}
OR the value ofuid
in this path? In any case, you should most probably be able to do all the processing in one Cloud Function. But we need more precise details in order to help you! – Renaud Tarnec