简体   繁体   English

在Firebase群组消息传递应用中实施已读回执功能

[英]Implement a read receipt feature in Firebase group messaging app

I'd like to implement a 'Seen' feature in my Firebase group messaging app. 我想在我的Firebase群组消息应用中实施“看到”功能。 Can you kindly advise the best and most efficient approach to take (working code will be appreciated)? 您能否建议采用最佳和最有效的方法(工作代码将受到赞赏)? For example, the app would show " Seen by 6 " or " Seen by 15 " on a group message. 例如,应用程序将在群组消息上显示“ 由6看到 ”或“ 由15看到 ”。

Here's my project: https://github.com/firebase/friendlychat/tree/master/android 这是我的项目: https//github.com/firebase/friendlychat/tree/master/android

Here's the MainActivity: https://github.com/firebase/friendlychat/blob/master/android/app/src/main/java/com/google/firebase/codelab/friendlychat/MainActivity.java 这是MainActivity: https//github.com/firebase/friendlychat/blob/master/android/app/src/main/java/com/google/firebase/codelab/friendlychat/MainActivity.java

To achieve this, you need to add another node in your Firebase database named seenBy which must be nested under each messageId in meassage section. 要实现此目的,您需要在名为seenBy的Firebase数据库中添加另一个节点,该节点必须嵌套在meassage部分中的每个messageId下。 Your database should look like this: 您的数据库应如下所示:

Firebase-root
   |
   ---- messages
           |
           ---- messageId1
                   |
                   ---- meessage: "Hello!"
                   |
                   ---- timestamp: 1498472455940
                   |
                   ---- seenBy
                          |
                          ---- userUid1: John
                          |
                          ---- userUid2: Mary
                          |
                          ---- userUid3: George

Every time a new user opens a meesage, just add the uid and the name as explained above. 每次新用户打开一个meesage时,只需添加uidname ,如上所述。

To implement Seen by 6 option, it's very easy. 要实现Seen by 6选项,这很容易。 You just need to create a listener and use getChildrenCount() method like this: 您只需创建一个listener并使用如下所示的getChildrenCount()方法:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference seenByRef = rootRef.child("messages").child(messageId).child("seenBy");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        long seenBy = dataSnapshot.getChildrenCount();
        Lod.d("TAG", "Seen by: " + seenBy);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
seenByRef.addListenerForSingleValueEvent(eventListener);

To know whether a message has been opened or not, you need to add another field to your user section in which you need to add a boolean with the default value of false . 要知道消息是否已打开,您需要在用户部分添加另一个字段,您需要在其中添加一个默认值为falseboolean值。 This new section should look like this: 这个新部分应如下所示:

Firebase-root
   |
   ---- users
       |
       ---- userId1
           |
           ---- meessages
              |
              ---- messageId1: false
              |
              ---- messageId2: false
              |
              ---- messageId3: false

When a users opens that message, just set the value of that particular message from false to true , which means that the particular message has been opened. 当用户打开该消息时,只需将该特定消息的值从falsetrue ,这意味着已打开特定消息。 This is the code: 这是代码:

DatabaseReference openedRef = rootRef.child("users").child(userId).child("meessages").child("messageId1");
openedRef.setValue(true);

When you create a message, use push() method on the reference like this: 创建消息时,对引用使用push()方法,如下所示:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference messageRef = rootRef.child("meessages").push();
String messageKey = messageRef.getKey();

Having this key, you can use it in your DatabaseReference . 拥有此密钥后,您可以在DatabaseReference使用它。 In the same way you can use for the userId. 以同样的方式可以用于userId。

Hope it helps. 希望能帮助到你。

Yeah it is possible, You can add a child seen_by to the message, whenever user views the message append the viewers userId to the seen_by , then by retrieving the userID and you can count the number of viewers and also you can see the list of viewed members too. 是的,有可能,你可以在消息中添加一个孩子seen_by ,每当用户查看消息将查看者userId附加到seen_by ,然后通过检索userID ,你可以计算观众的数量,你也可以看到查看的列表成员也是。 The format should be 格式应该是

message:{
    FirebaseKey:{
        message:"hi",
        timestamp:1498472455940,
        seen_by:"abc,def,ghi" //users UID
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM