0

Trying to implement a read receipt feature in my app...

Posting Data

private void sendMessage() {
    String messageText = messageArea.getText().toString();
    if (TextUtils.isEmpty(messageText)) {
        Toast.makeText(getApplicationContext(), "Can't Send Blank Message", Toast.LENGTH_SHORT).show();
    } else {

        String message_sender_ref = "Messages/" + MessageSenderId + "/" + MessageRecieverId;
        String message_reciver_ref = "Messages/" + MessageRecieverId + "/" + MessageSenderId;

        Map messageTextBody = new HashMap<>();
        messageTextBody.put("Message", messageText);
        messageTextBody.put("Seen", "False");
        messageTextBody.put("Type", "Text");
        messageTextBody.put("Time", ServerValue.TIMESTAMP);
        messageTextBody.put("From", MessageSenderId);

        DatabaseReference user_message_key = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).push();
        String message_push_id = user_message_key.getKey();

        Map messageBodyDetails = new HashMap();
        messageBodyDetails.put(message_sender_ref + "/" + message_push_id, messageTextBody);
        messageBodyDetails.put(message_reciver_ref + "/" + message_push_id, messageTextBody);

        mDatabaseReference.updateChildren(messageBodyDetails, new DatabaseReference.CompletionListener() {
            @Override
            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                if (databaseError != null) {
                    Log.d("Chat_Log", databaseError.getMessage().toString());
                }
                messageArea.setText("");
            }
        });
    }
}

Now how do i access the "Seen = False" data which is inside a unique ID... i can access it through ValueEventListener but how do i make changes of that? i know only to fetch the data but i want to change the data of it... Can someone help me out please

Tried method

     DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child("Seen");
    seenRef.setValue("True");

I tried this above method but it just creates one more hild alongside it and sets its value to true.... someone please help me out... Thanks in advance

Database - https://ibb.co/js3iDd

  • Could you please add an image of your database? – orimdominic Jul 16 '18 at 11:53
  • Look to my edited question please –  Jul 16 '18 at 11:58
  • Well, from what I can see from the picture of your database, the `seenRef.setValue(True);` creates another path. You are not accessing the same path for the message when updating the `seen ` property as you are when pushing the message in your database – orimdominic Jul 16 '18 at 12:16

2 Answers2

1

This method you are trying to use

 DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child("Seen");
    seenRef.setValue("True");

is replacing or overwriting your current value at that reference, i would suggest to make a map and use updateChildren to just update the value at the desired reference, also if you need to update multiple values at the same reference, this will save you multiple setValues()

Map<String, Object> update = new HashMap<String,Object>();
update.put("Seen", true);
seenRef.updateChildren(update);

but you can also do the same as you are doing without "" in your setValue() because you want to poot a boolean true or false inside it and you are sending a string "True" to the database , this will create a new key replacing/overwriting that same ref with the value Seen : true

DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child("Seen");
    seenRef.setValue(true);

Please check your seenRef, i just checked your db and i think is missing one more ref

enter image description here

so just change your ref

DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child(your_push_id_message).child("Seen");

in order to get your_push_id_message you will need to get that push id of the message to do that, first attach a listener to it and getKey()

seenRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
     for(DataSnapshot snapshot : dataSnapshot.getChildren()){

      String pushMessage = snapshot.getKey();
      dataSnapshot.child(pushMessage).child("Seen").setValue(true);



}

  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    System.out.println("The read failed: " + databaseError.getCode());
  }
});
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • That's right... how do i access that... what will 'your_push_id_message' be equal to... i mean i need to declare it but i dont know cuz its unique and automatically generated.. so how do i do that –  Jul 16 '18 at 12:37
  • you will need to create a value event listener and use getKey() in order to get the keys of each post, check this https://stackoverflow.com/questions/37094631/get-the-pushed-id-for-specific-value-in-firebase-android – Gastón Saillén Jul 16 '18 at 12:37
  • also you will need to create your updateChildren or setValue inside this listener – Gastón Saillén Jul 16 '18 at 12:38
0

You are doing wrong because you are the directly updating value not with message-id which you generating for the message using push()

Here you doing

DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child("Seen");
    seenRef.setValue("True");

But you are not referring message id in which you need to do.

Like bellow

DatabaseReference seenRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).child(Message-Id).child("Seen");
    seenRef.setValue("True");
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39