I have the below function for private chat notifications in Flutter. I’d like to do the same for a group chat where there are multiple recipentids. How can I achieve that by having multiple recipientids in an array so all the users (except the sender) get notified?
gmNotification(String recipientid) {
String notificationid = const Uuid().v1();
String res = "Some error occurred";
try {{
FirebaseFirestore.instance
.collection('notifications')
.doc(recipientid)
.collection('private-message-notifications')
.doc(notificationid)
.set({
'notificationid': notificationid,
'timestamp': DateTime.now(),
},
);
}
res = 'success';
} catch (err) {
res = err.toString();
}
return res;
}
>Solution :
You can modify your gmNotification function to accept an array of recipientids
instead of a single recipientid
, and then loop through the array to create notifications for each recipient. Here’s an example:
gmGroupNotification(List<String> recipientids) {
String notificationid = const Uuid().v1();
String res = "Some error occurred";
try {
for (String recipientid in recipientids) {
FirebaseFirestore.instance
.collection('notifications')
.doc(recipientid)
.collection('group-message-notifications')
.doc(notificationid)
.set({
'notificationid': notificationid,
'timestamp': DateTime.now(),
});
}
res = 'success';
} catch (err) {
res = err.toString();
}
return res;
}