Firebase数据库如何从WhatsApp Chats页面等节点创建列表

时间:2017-09-07 08:57:23

标签: android firebase firebase-realtime-database android-recyclerview recycler-adapter

我正在尝试使用Firebase数据库创建聊天应用。我正在阅读文档和观看教程,但有一件事我无法弄清楚该怎么做。当用户向其他用户发送消息时,使用密钥创建聊天室:" senderUserId_receiverUserId"
您可以在下面看到我的结构。

{
  "chat_rooms" : {
    "nTAHqCTmLRcLOM8CTfnHF4lRjLf2_oTLYaHMOMibh3ZqOcmpcWDtSCKp1" : {
      "-KtQEGK38lhZrgnNxmqb" : {
        "date" : "07/09/2017 10:28",
        "message" : “Thanks for helping !”,
        "photoUrl" : "http://www.clker.com/cliparts/B/R/Y/m/P/e/blank-profile-hi.png",
        "receiverName" : "Ali”,
        "receiverUid" : "oTLYaHMOMibh3ZqOcmpcWDtSCKp1",
        "senderName" : “John”,
        "senderUid" : "nTAHqCTmLRcLOM8CTfnHF4lRjLf2"
      },
      "-KtQEKK2BmIMzwruN-21" : {
        "date" : "07/09/2017 10:28",
        "message" : “Another Test Message“,
        "photoUrl" : "http://www.clker.com/cliparts/B/R/Y/m/P/e/blank-profile-hi.png",
        "receiverName" : “John”,
        "receiverUid" : "nTAHqCTmLRcLOM8CTfnHF4lRjLf2",
        "senderName" : "Ali",
        "senderUid" : "oTLYaHMOMibh3ZqOcmpcWDtSCKp1"
      },
      "-KtQIGDk5zE4JZuE9pIQ" : {
        "date" : "07/09/2017 10:45",
        "message" : “Test message !“,
        "photoUrl" : "http://www.clker.com/cliparts/B/R/Y/m/P/e/blank-profile-hi.png",
        "receiverName" : “John”,
        "receiverUid" : "nTAHqCTmLRcLOM8CTfnHF4lRjLf2",
        "senderName" : "Ali",
        "senderUid" : "oTLYaHMOMibh3ZqOcmpcWDtSCKp1"
      }
    }
  },
  "users" : {
    "nTAHqCTmLRcLOM8CTfnHF4lRjLf2" : {
      "address" : “istanbul”,
      "cell_phone" : “none”,
      "email" : “john@gmail.com",
      "home_phone" : “none”,
      "name" : “John”,
      "photoUrl" : "http://www.clker.com/cliparts/B/R/Y/m/P/e/blank-profile-hi.png",
      "userId" : "nTAHqCTmLRcLOM8CTfnHF4lRjLf2"
    },
    "oTLYaHMOMibh3ZqOcmpcWDtSCKp1" : {
      "address" : “istanbul”,
      "cell_phone" : “none”,
      "email" : "ali@gmail.com”,
      "home_phone" : “none”,
      "name" : "Ali",
      "photoUrl" : "http://www.clker.com/cliparts/B/R/Y/m/P/e/blank-profile-hi.png",
      "userId" : "oTLYaHMOMibh3ZqOcmpcWDtSCKp1"
    }
  }
}

通过这种方式一切正常,但我不知道如何在WhatsApp的聊天页面列出用户列表中的所有会话。我的意思是当用户点击时对话项目相关对话将会公开 我发送的私信包含以下代码:

public void sendMessageToFirebaseUser(final Context context, final ChatMessageModel chat, final String receiverFirebaseToken) {

        final String room_type_1 = chat.getSenderUid() + "_" + chat.getReceiverUid();
        final String room_type_2 = chat.getReceiverUid() + "_" + chat.getSenderUid();

        final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

        databaseReference.child(Constants.ARG_CHAT_ROOMS)
                .getRef()
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.hasChild(room_type_1)) {
                            Log.e("aaa", "sendMessageToFirebaseUser: " + room_type_1 + " exists");
                            databaseReference.child(Constants.ARG_CHAT_ROOMS)
                                    .child(room_type_1)
                                    .push()
                                    .setValue(chat);
                        } else if (dataSnapshot.hasChild(room_type_2)) {
                            Log.e("aaa", "sendMessageToFirebaseUser: " + room_type_2 + " exists");
                            databaseReference.child(Constants.ARG_CHAT_ROOMS)
                                    .child(room_type_2)
                                    .push()
                                    .setValue(chat);
                        } else {
                            Log.e("aaa", "sendMessageToFirebaseUser: success");
                            databaseReference.child(Constants.ARG_CHAT_ROOMS)
                                    .child(room_type_1)
                                    .push()
                                    .setValue(chat);
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
    }

感谢您阅读我的帖子。

1 个答案:

答案 0 :(得分:0)

如果要显示当前用户的聊天室列表,您应该对数据建模以允许这样做。最简单的方法是为每个用户添加一个聊天室列表到您的数据模型中:

"userChatrooms" : {
  "nTAHqCTmLRcLOM8CTfnHF4lRjLf2" : {
    "nTAHqCTmLRcLOM8CTfnHF4lRjLf2_oTLYaHMOMibh3ZqOcmpcWDtSCKp1": true
  },
  "oTLYaHMOMibh3ZqOcmpcWDtSCKp1" : {
    "nTAHqCTmLRcLOM8CTfnHF4lRjLf2_oTLYaHMOMibh3ZqOcmpcWDtSCKp1": true
  }
}