这是我过滤用户联系人的地方,我这样做是为了只获取用户的联系人,如果他们创建了一个帐户,那么它们将显示在您的屏幕上。(还有更多,但这只是为了更直观(
List<PhonesContacts> phoneContacts = snapshot.data;
List myContacts = [];
for (var j = 0; j < phoneContacts.length; j++) {
myContacts.contains(phoneContacts[j])
? null
: myContacts.add(phoneContacts[j]);
}
My Question:
我是否需要在后端(云函数(中执行上述相同的操作来过滤用户的令牌,以便我只能将通知发送给您的联系人?或者有没有办法从前端到后端进行通信?这样我就可以将myContacts数组发送到后端,然后我只需要过滤令牌即可。
这是我为云功能所做的,我知道我现在拥有的会获取每个人的令牌并向每个人的设备发送通知。所以再次,问题,如何只向仅在屏幕上显示的用户发送通知(这是您的联系人,因为我在前端过滤了它们(
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
const db = admin.firestore();
const fcm = admin.messaging();
export const sendToDevice = functions.firestore
.document('stat/{statId}')
.onUpdate(async change => {
const data = change.after.data();
const currentUser = await db
.collection("profile")
.doc(data.uid)
.get();
const name = currentUser.get("name");
const querySnapshot = await db
.collection('tokens')
.get();
/* Somewhere here I would access their contacts and filter it, then get their tokens and put it in the array OR if I can, I would just send the already filtered contacts I did in the front end, then just put the tokens in the array */
const tokens = querySnapshot.docs.map(snap => snap.data().token);
const payload: admin.messaging.MessagingPayload = {
notification: {
title: name,
body: data.stat,
click_action: 'FLUTTER_NOTIFICATION_CLICK'
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
title: name,
body: data.stat
}
};
return fcm.sendToDevice(tokens, payload);
});
我找到了一个包,实际上允许您从前端到后端进行通信。使用 cloud_functions 颤振包 https://pub.dev/packages/cloud_functions