我正在尝试为用户编写云功能以获取通知。
我有一个任务模型,其中用户指定一个任务,并写入他向其分配任务的另一个用户的电子邮件ID。
添加上述任务后,应将通知发送给“ Taskgivento”字段中指定的用户
我的用户模型如下
我有一个用户模型,其中包含用户和子集合的数据,将fcm保存在令牌子集合中。该子集合的ID是fcm令牌。
我的云功能
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);
});
在const queryshot中,我试图过滤用户模型以获取fcm令牌,以使其将通知Taskgiven的uid通知给emailid。
以上功能有效。 我已经在上述模型中手动编写了uid。 我需要它来搜索数据库并填充taskgivento电子邮件的uid。 我应该如何进行 我尝试像在飞镖中一样编写where('email','=','Taskdata.Taskgivento'),但没有用。它给出了一个错误。
答案 0 :(得分:3)
以下应该可以解决问题:
export const sendToDevice = functions.firestore
.document('Task/{TaskId}')
.onCreate(async snapshot => {
const Taskdata = snapshot.data()
const email = Taskdata.Taskgivento;
const userQuerySnapshot = await db
.collection('users')
.where('email', '==', email)
.get();
const userDocSnapshot = userQuerySnapshot.docs[0]; // Assumption: there is only ONE user with this email
const userDocRef = userDocSnapshot.ref;
const tokensQuerySnapshot = await userDocRef.collection('tokens').get();
const tokens = tokensQuerySnapshot.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'
}
};
await fcm.sendToDevice(tokens, payload);
return null;
});
您需要:
where('email', '==', email)
与==
的语法。DocumentReference
,并根据本参考资料查询tokens
子集合。