简体   繁体   中英

Message you send to other user doesn't show up in their ChatsFragment. Only appears for sender of the message not for receiver

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:

在此处输入图像描述

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) {

            }
        });
    }

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM