Firebase一对一聊天android [英] Firebase one to one chat android
问题描述
我对创建一对一firbase聊天应用程序感到困惑.
I am bit confused to create one to one firbase chat application .
我的要求是我需要显示每个用户的聊天列表(用户聊天的历史记录).用户从列表中单击一个用户后,我要显示与所选用户的一对一聊天窗口.
My requirement is i need to show the chat list for each users(History of user chats). Once the user clicks one user from list i want to show the one to one chat window with selected user.
但是使用我的格式,当我向任何用户发送消息时,所有用户都收到了该消息,并且我无法显示特定用户的过滤后的聊天列表,它显示了Firebase数据库中的所有聊天记录
but with my format when i send a message to any user all the user got that message and i cant show filtered chat list for particular user it show all the chat history from firebase database
这是我在firebase中的json结构
this is my json strucure in firebase
Java代码
static final String CHAT_REFERENCE = "lifegoal";
ChatFirebaseAdapter firebaseAdapter = new ChatFirebaseAdapter(mFirebaseDatabaseReference.child(CHAT_REFERENCE), userModel.getName(), this);
rvListMessage.setLayoutManager(mLinearLayoutManager);
rvListMessage.setAdapter(firebaseAdapter);
推荐答案
首先,您必须更改数据库的结构方式,以便可以为每个一对一的聊天创建新的线程或会话ID.例如,您可以尝试如下操作:
First, you'll have to change the way your database is structured so that you can create a new thread or conversation id for each one to one chat. For example, you can try something like this:
- lifegoal
- senderId-receiverId
- KNa0...
- message
- timestamp
+ usermodel
这将允许您创建两个用户唯一的新threadId.
This will allow you create a new threadId unique to both users.
唯一的警告是,您需要检查Firebase中哪个孩子有效,才能知道要使用哪个孩子.例如,您可以尝试执行以下操作:
The only caveat is that you'll need to check which child is valid in firebase to know which one to use. For instance, you can try something like this:
字符串类型_1 = senderId +"_" + receiverId;字符串type_2 =接收者ID +"_" +发送者ID;
然后按如下所示签入 onDataChange()
回调:
And then check in your onDataChange()
callback like below:
`@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(type_1)) {
// do something...
} else if (dataSnapshot.hasChild(type_2)) {
// do something...
} else {
// do something...
}
}`
这篇关于Firebase一对一聊天android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!