I'm making a chat app. Person A sends a message to B, which is clearly visible when viewing the chat room on A's device. But when I go to B's device and look at the chat window, I can't see the message sent by A. I know what the problem is, but I don't know how to fix it. please I need help.
My database has the following structure:
"chatrooms" : {
"-Mn0ZDpdWsOb8XFFTYmi" : {
"comments" : {
"-Mn0ZG0UoMY81i9sf6nN" : {
"message" : "hi",
"timestamp" : 1635335082263,
"uid" : "QTC8Msw1n2aUF8mDbTCUNk5k7sH3"
}
},
"users" : {
"itemName" : "goods",
"sellerUid" : "vzTQG0Fb3DgsVKGgGj0R23uL6Hx2",
"uid" : "QTC8Msw1n2aUF8mDbTCUNk5k7sH3"
}
}
It's a problematic code:
FirebaseDatabase.getInstance().getReference().child("chatrooms")
.orderByChild("users/uid").equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
chatModels.clear();
for (DataSnapshot item : snapshot.getChildren()){
chatModels.add(item.getValue(ChatModel1.class));
}
notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
============
FirebaseDatabase.getInstance().getReference().child("chatrooms")
.orderByChild("users/sellerUid").equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
chatModels.clear();
for (DataSnapshot item : snapshot.getChildren()){
chatModels.add(item.getValue(ChatModel1.class));
}
notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
This shows A's message on B's device, which is far from solving the problem.
so, What I want is:
FirebaseDatabase.getInstance().getReference().child("chatrooms") .orderByChild("users/uid").equalTo(uid).addListenerForSingleValueEvent
In this part, we want not only users/uid but also users/cellerUid to be searched and the euqalTo(udi) condition is executed.