我尝试使用mvp开发一对一的聊天应用程序 当应用程序第一次运行时,聊天工作正常,并且会向用户发送消息。 问题是,当我尝试注册另一个帐户并尝试发送消息时,firebase创建了两个不同的聊天室,我无法获取消息,因为第二个用户没有检测到聊天室已创建并创建另一个 你帮我吗? 这是我尝试发送消息的代码:
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().addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(room_type_1)) {
Log.e(TAG, "sendMessageToFirebaseUser: " + room_type_1 + " exists");
databaseReference.child(Constants.ARG_CHAT_ROOMS).child(room_type_1).child(String.valueOf(chat.getTimestamp())).setValue(chat);
} else if (dataSnapshot.hasChild(room_type_2)) {
Log.e(TAG, "sendMessageToFirebaseUser: " + room_type_2 + " exists");
databaseReference.child(Constants.ARG_CHAT_ROOMS).child(room_type_2).child(String.valueOf(chat.getTimestamp())).setValue(chat);
} else {
Log.e(TAG, "sendMessageToFirebaseUser: success");
Log.e("sender uis",""+chat.getSenderUid());
Log.e("srevier uis",""+chat.getSenderUid());
databaseReference.child(Constants.ARG_CHAT_ROOMS).child(room_type_1).child(String.valueOf(chat.getTimestamp())).setValue(chat);
getMessageFromFirebaseUser(chat.getSenderUid(), chat.getReceiverUid());
}
sendPushNotificationToReceiver(chat.sender,
chat.message,
chat.senderUid,
new SharedPrefUtil(context).getString(Constants.ARG_FIREBASE_TOKEN),
receiverFirebaseToken);
mOnSendMessageListener.onSendMessageSuccess();
}
@Override
public void onCancelled(DatabaseError databaseError) {
mOnSendMessageListener.onSendMessageFailure("Unable to send message: " + databaseError.getMessage());
}
});
}
这是我从firebase获取消息的代码
public void getMessageFromFirebaseUser(final Context context,String senderUid,String receiverUid,final ChatFragmentListener chatFragmentListener){
final String room_type_1 = senderUid + "_" + receiverUid;
final String room_type_2 = receiverUid + "_" + senderUid;
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
final DatabaseReference databaseChats = FirebaseDatabase.getInstance().getReference().child(Constants.ARG_CHAT_ROOMS);
databaseChats.keepSynced(true);
databaseReference.child(Constants.ARG_CHAT_ROOMS).getRef().addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(room_type_1)) {
Log.e(TAG, "getMessageFromFirebaseUser: " + room_type_1 + " exists");
FirebaseDatabase.getInstance()
.getReference()
.child(Constants.ARG_CHAT_ROOMS)
.child(room_type_1).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getKey() != s) {
Chat chat = new Chat();
chat = dataSnapshot.getValue(Chat.class);
chatFragmentListener.onGetMessagesSuccess(chat);
Log.e("1", "get");
}
// do something here
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
/* Log.e("childadded","eeeee"+s);
Log.e("chilchanged","ee"+dataSnapshot.getKey());
Chat chat=dataSnapshot.getValue(Chat.class);
String key=dataSnapshot.getKey();
if(!key.equals(s)){chats.add(chat);}*/
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
/* Log.e("childadded","eeeee"+s);
Log.e("childmoved","ee"+dataSnapshot.getKey());
Chat chat=dataSnapshot.getValue(Chat.class);
String key=dataSnapshot.getKey();
if(key.equals(s)){chats.remove(chat);}*/
}
@Override
public void onCancelled(DatabaseError databaseError) {
// lll
chatFragmentListener.onGetMessagesFailure("Unable to get message: " + databaseError.getMessage());
}
});
} else if (dataSnapshot.hasChild(room_type_2)) {
Log.e(TAG, "getMessageFromFirebaseUser: " + room_type_2 + " exists");
FirebaseDatabase.getInstance()
.getReference()
.child(Constants.ARG_CHAT_ROOMS)
.child(room_type_2).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Chat chat = new Chat();
chat = dataSnapshot.getValue(Chat.class);
chatFragmentListener.onGetMessagesSuccess(chat);
Log.e("2", "get");
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
chatFragmentListener.onGetMessagesFailure("Unable to get message: " + databaseError.getMessage());
}
});
} else {
Log.e(TAG, "getMessageFromFirebaseUser: no such room available");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
chatFragmentListener.onGetMessagesFailure("Unable to get message: " + databaseError.getMessage());
}
});
}
答案 0 :(得分:0)
此问题的最简单解决方案是通过按升序或降序排列用户uuid来创建聊天室。
public String getChatRoom(String fromUserId,String toMessageId){
String chatRoom;
if(fromUserId.compareTo(toMessageId) < 0 ){
// ToMessageId has less alphabetic order then fromUserId
chatRoom = toMessageId+" "+fromUserId;
}else{
chatRoom = fromUserId+" "+toMessageId;
}
return chatRoom;
}
Here是firebase聊天应用的示例实现