Home > Software design >  How to show count of unread messages for a particular user with firebase flutter as shown in image
How to show count of unread messages for a particular user with firebase flutter as shown in image

Time:12-27

Image here

I have searched a lot but only getting unread message badges for app icon

Please help me get this

CodePudding user response:

Retrieve lists of items or listen for additions to a list of items. This event is triggered once for each existing child and then again every time a new child is added to the specified path. The listener is passed a snapshot containing the new child's data(onChildAdded).

final commentsRef = FirebaseDatabase.instance.ref("post-comments/$postId");
commentsRef.onChildAdded.listen((event) {
  // A new comment has been added, so add it to the displayed list.
});
commentsRef.onChildChanged.listen((event) {
  // A comment has changed; use the key to determine if we are displaying this
  // comment and if so displayed the changed comment.
});
commentsRef.onChildRemoved.listen((event) {
  // A comment has been removed; use the key to determine if we are displaying
  // this comment and if so remove it.
});

Docs

CodePudding user response:

Your firebase should have a key such as isRead to indicate whether the message was read. By default it should be false.

When the user reads a message, change isRead to true.

To get the unread messages count, loop through the messages and count the number of isRead == true.

  • Related