0

Having trouble with the logic for the following method, maybe someone could help me out. When a user sends another user a message the message appears in the ChatsFragment like it's supposed to for the user who sent the message, but the other user doesn't get it / see it. The messages only appear if you were the one that sent the first message.

I would like to make it so that the other user receives the message even if they don't respond to it. Shouldn't the else if statement in the ChatsFragment account for what I am aiming to do? If the user currently logged in is the id (receiver), then they should also have the message appear in their ChatsFragment even though they haven't responded to it yet? Or am I missing something?

The JSON structure looks like this:

enter image description here

To send the message I use this method in MessageActivity:

MessageActivity

private void sendMessage(String sender, final String receiver, String message) {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats");

        Date date = Calendar.getInstance().getTime();
        String formattedDate = DateFormat.getDateInstance(DateFormat.FULL).format(date);

        String time = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            time = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm"));
        }

        String messageId = reference.push().getKey();

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("sender", sender);
        hashMap.put("receiver", receiver);
        hashMap.put("message", message);
        hashMap.put("isseen", false);
        hashMap.put("date", formattedDate);
        hashMap.put("time", time);
        hashMap.put("timestamp", ServerValue.TIMESTAMP);
        hashMap.put("messageId", messageId);

        if (messageId != null)
            reference.child(messageId).setValue(hashMap);

        final DatabaseReference chatref = FirebaseDatabase.getInstance().getReference("Chatlist").child(mFirebaseUser.getUid()).child(mId);
        chatref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (!dataSnapshot.exists()) {
                    chatref.child("id").setValue(mId);
                    chatref.child("sender").setValue(mFirebaseUser.getUid());
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

And to add the message to the ChatFragment I use the following method which works to add the chat if you are the one to initiate conversation, but as I said before the other user doesn't receive it:

ChatsFragment

private void chatList() {
    mUsers = new ArrayList<>();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mUsers.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    User user = snapshot.getValue(User.class);
                    for (Chatlist chatlist : mUsersList) {
                        if (mFirebaseUser != null && user != null) {
                            if (!user.getId().equals(mFirebaseUser.getUid()) && user.getId().equals(chatlist.id)) {
                                mUsers.add(user);
                            } else if (mFirebaseUser.getUid().equals(chatlist.id)) {
                                mUsers.add(user);
                            }
                        }
                    }
                }

                mUserAdapterChat = new UserAdapterChat(getContext(), mUsers, true);
                mUserAdapterChat.notifyDataSetChanged();
                mRecyclerView.setAdapter(mUserAdapterChat);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
JMB
  • 313
  • 2
  • 9
  • Create chatId amigo. And add sender and receiver to chatId node. follow [this](https://stackoverflow.com/a/44552371/8956604) – Kasım Özdemir May 07 '20 at 15:54
  • @KasımÖzdemir Hey, I´ll give it a shot. Let's see if I do it right – JMB May 07 '20 at 16:01
  • @KasımÖzdemir so I changed the JSON structure to reflect what the guy did in the post above, is this one okay? I'm still having trouble with the method though. The message isn't showing up for either user now... Before it was showing up for the user who sent the message, but now neither one – JMB May 09 '20 at 18:53

1 Answers1

1

John, this is my Firebase Database. It is similar to the answer I suggested to you. You can examine. It may be more useful for you.

enter image description here

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35