I am sending a notification using .onCreate
on a collection.
My collection is tasks
. When new task is given, I need to send a notification to the assigned person.
Do I need to save token
of this assigned person? I can only fetch those values which are getting saved in document. The value of token
is in users
collection.
Is there a way to use .where
condition in functions ?
My code so far [saving token of assigned person in assignTo
field with every new task entry]::
.document('todos/{todosId}')
.onCreate(
async (snapshot: { data: () => { (): any; new(): any; token: any; assignTo: any; text: any; name: any; profilePicUrl: any; }; }) => {
// Notification details.
const text = snapshot.data();
const payload = {
notification: {
title: text.name,
body: 'Notification body',
icon: 'https://img.icons8.com/material/4ac144/256/user-male.png',
click_action: `https://google.com`,
}
};
const subscriber = text.token;
return admin.messaging().sendToDevice(subscriber, payload)
});
Is .where
condition possible ? like,
collections 'users' get
token
whereassignTo
==dwqhdiu78798u
Edit
As guided here is my code, but function gave error
// const subscriber = "evGBnI_klVQYSBIPMqJbx8:APA91bEV5xOEbPwF4vBJ7mHrOskCTpTRJx0cQrZ_uxa-QH8HLomXdSYixwRIvcA2AuBRh4B_2DDaY8hvj-TsFJG_Hb6LJt9sgbPrWkI-eo0Xtx2ZKttbIuja4NqajofmjgnubraIOb4_";
const query = admin.firestore().collection('Students').where('Name', '==', 'caca');
const querySnapshot = await query.get();
const subscriber = querySnapshot.doc.data().token;
return admin.messaging().sendToDevice(subscriber, payload)
});
Screenshot attached:
Error in function: