Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter Firebase Group Inapp Message Notification

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading