As I work on developing a cloud function to send notifications to users, I encounter the challenge of correctly implementing this feature. In my Task model, a user specifies a task and assigns it to another user by providing their email address. https://i.sstatic.net/8Zcc5.png
Upon adding a task, the system should notify the user specified in the 'Taskgivento' field.
In my user model structure https://i.sstatic.net/S2A7K.png I have a collection of users storing user data, with a subcollection for FCM tokens identified by the token ID.
Below is my cloud function code:
export const sendToDevice = functions.firestore
.document('Task/{TaskId}')
.onCreate(async snapshot => {
const Taskdata=snapshot.data()
const querySnapshot = await db
.collection('users')
.doc('HMPibf2dkdUPyPiDvOE80IpOsgf1')
.collection('tokens')
.get();
const tokens = querySnapshot.docs.map(snap => snap.id);
const payload: admin.messaging.MessagingPayload = {
notification: {
title: 'New Order!',
body: 'new notification',
icon: 'your-icon-url',
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
return fcm.sendToDevice(tokens, payload);
});
Within the querySnapshot, I aim to retrieve the FCM token from the user model that matches the email specified in the 'Taskgivento' field. Despite manually entering the UID in the model, I strive to dynamically fetch the UID based on the email given for Taskgivento. However, attempts using 'where('email','=','Taskdata.Taskgivento')', similar to Dart syntax, yield errors.