Reputation: 27
In my chat app.i want to receive only recently added messages so i am trying to receive message via query startAt() but the query return no data.
please help me to find out where i making a mistake:
this is my data structure: (My Datastructure image)
-Chats
-TrHRsy8x7WYZjodQrNrzfumw16z2g2dojhGBdie07PXqhl47l8GWN1a2
-LastMessage
-Messages
-MCi-gOmhLUl5MuLtCiI
-MCi-hstmFyAfAczexvI
LT: -11108656
T: 1595278548931
c: 2
m: "hi"
r: "g2dojhGBdie07PXqhl47l8GWN1a2"
s: "TrHRsy8x7WYZjodQrNrzfumw16z2"
-MCi-kukmwJpU3ubswId
Query ref2 = FirebaseDatabase.getInstance().getReference("Chats/" + uniquechatid()).child("Messages").orderByChild("T").startAt("1595278548931");
receiveListener = ref2.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName)
{
if (snapshot.getValue() != null) {
Chat chat = snapshot.getValue(Chat.class);
assert chat != null;
Log.e("chat added",chat.getM());
}
}
});
i want to receive only messages from those timestamp.i even tried the startAt(value,key) .startAt() doesnt work.if i remove startAt() then data receives perfectly.
my message uploading:
private void sendMessage(String sender, String receiver, String message) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
HashMap<String, Object> hashmap = new HashMap<>();
hashmap.put("s", sender);
hashmap.put("r", receiver);
hashmap.put("m", message);
hashmap.put("c", 1);
hashmap.put("T",ServerValue.TIMESTAMP);
hashmap.put("LT",timenow);
reference.child("Chats/"+uniquechatid()+"/Messages").push().setValue(hashmap);
}
my database rules:
{
"rules": {
".read": true,
".write": true,
"Chats": {
"$chatid":{
"Messages":{
"$messageid":{
".indexOn": "T"
}
}
}
}
}
}
Upvotes: 0
Views: 135
Reputation: 388
There is a child of Messages in the pic. You ref2 should be like
Query ref2 = FirebaseDatabase.getInstance().getReference("Chats/" + uniquechatid()).child("Messages/" +"MCi-hstmFyAfAczexvI").orderByChild("T").startAt("1595278548931");
receiveListener = ref2.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName)
{
if (snapshot.getValue() != null) {
Chat chat = snapshot.getValue(Chat.class);
assert chat != null;
Log.e("chat added",chat.getM());
}
}
});
provide message id or the id you are using to store different messages as the child of Messages.
Upvotes: 1