2016-07-26 148 views
0

我有点困惑创建一对一的firbase聊天应用程序。Firebase一对一聊天android

我的要求是我需要显示每个用户的聊天列表(用户聊天记录)。一旦用户从列表中单击一个用户,我想显示与选定用户的一对一聊天窗口。

但我的格式,当我将消息发送到任何用户的所有用户得到了消息,我不能显示特定用户过滤聊天列表中显示它的所有聊天记录,从火力数据库

这是我的JSON strucure在火力

enter image description here

Java代码

static final String CHAT_REFERENCE = "lifegoal"; 

ChatFirebaseAdapter firebaseAdapter = new ChatFirebaseAdapter(mFirebaseDatabaseReference.child(CHAT_REFERENCE), userModel.getName(), this); 
    rvListMessage.setLayoutManager(mLinearLayoutManager); 
    rvListMessage.setAdapter(firebaseAdapter); 

回答

-1

我认为你的数据保存格式是错误的。您正在以全线程发送消息,例如lifegoal,您需要将其发送到存储单个用户转换的单个线程。看看这些链接

Firebase : Build a real time chat
Firebase Chat for Android
bonfire-firebase-sample

上述链接将演示如何火力聊天的作品,你应该遵循什么样的DataModel的。

Here他们表现出的数据结构,你可以遵循

+1

,但如果我将其存储在单个线程浩我可以访问它了特定的孩子 – Tufan

3

首先,你必须改变你的数据库结构的方式,让你可以为每个一对一聊天一个新的线程或对话id 。例如,你可以尝试这样的事:

- lifegoal 
    - senderId-receiverId 
     - KNa0... 
      - message 
      - timestamp 
      + usermodel 

这将允许您创建一个新的threadId独特的两个用户。

唯一需要注意的是,您需要检查哪个孩子在firebase中有效,才能知道使用哪个孩子。举例来说,你可以尝试这样的事:

String type_1 = senderId + "_" + receiverId; String type_2 = receiverId + "_" + senderId;

,然后检查你的onDataChange()回调象下面这样:

`@Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     if (dataSnapshot.hasChild(type_1)) { 
      // do something... 
     } else if (dataSnapshot.hasChild(type_2)) { 
      // do something... 
     } else { 
      // do something... 
     } 
    }`