When filtering the user's contacts, I ensure that only contacts with created accounts are displayed on the screen. This process helps in visually organizing the contact list.
List<PhonesContacts> phoneContacts = snapshot.data;
List myContacts = [];
for (var j = 0; j < phoneContacts.length; j++) {
myContacts.contains(phoneContacts[j])
? null
: myContacts.add(phoneContacts[j]);
}
Query:
Do I need to replicate the above process in the backend using cloud functions to filter the user's tokens before sending notifications to their contacts? Alternatively, is there a way to pass the myContacts array from frontend to backend for direct token filtering?
In my current cloud function setup, all tokens receive notifications regardless of whether they are relevant contacts or not. How can this function be modified to target specific users based on the frontend filtered contacts?
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();
/* Here, I would implement contact filtration and token retrieval OR explore passing pre-filtered contacts from the frontend for token inclusion */
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);
});