i try to develop one to one chat application using mvp when the application run for the first time the chat works correctly and messsges are sent to users . the problem is when i try to register with another account and try to send messages firebase creates two different chat rooms and i can't get messages because the second user didn't detect that a chat room has been created and create an other one cn you help me please ?? This the code where i try to send message:
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());
}
});
}
and this is the code where i get message from 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());
}
});
}
.