无法在“聊天应用程序”中的最近聊天中接收聊天

时间:2020-03-11 17:36:05

标签: android firebase firebase-realtime-database arraylist

我正在创建聊天应用程序,并且正在使用android studio和firebase实时数据库,并且试图在两个用户之间进行聊天...但是在发件人向收件人发送消息时,发件人可以在“最近的聊天”中看到用户名消息标签“,但在接收方,它无法在“最近的聊天”中接收发件人名称,所有聊天都将存储在Firebase实时数据库中,但无法在接收方显示消息。

    FirebaseUser firebaseUser;
    DatabaseReference databaseReference;
    String userid;

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            intent=getIntent();
            userid=intent.getStringExtra("userid");
            firebaseUser= FirebaseAuth.getInstance().getCurrentUser();

            btn_send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    notify = true;
                    String  msg=text_send.getText().toString();
                    if(!msg.equals(""))
                    {
                        sendMessage(firebaseUser.getUid(),userid, msg);
                    }else {
                        Toast.makeText(MessageActivity.this,"Can't Send Empty Messaage",Toast.LENGTH_LONG).show();
                    }
                    text_send.setText("");
                }
            });

    }

    // sendMessage Method

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

            HashMap<String, Object> hashMap=new HashMap<>();
            hashMap.put("sender", sender);
            hashMap.put("receiver", receiver);
            hashMap.put("message", message);
            hashMap.put("isseen",false);

            databaseReference.child("Chats").push().setValue(hashMap);

//This portion is not working


            final DatabaseReference chatref=FirebaseDatabase.getInstance().getReference("Chatlist")
                    .child(firebaseUser.getUid())
                    .child(userid);

            chatref.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (!dataSnapshot.exists()) {
                        chatref.child("id").setValue(userid);
                    }
                }

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

                }
            });

//ChatFragment

public class ChatsFragment extends Fragment {

    private RecyclerView recyclerView;
    private UserAdapter userAdapter;
    private List<User> musers;

    FirebaseUser firebaseUser;
    DatabaseReference databaseReference;

    private List<Chatlist> userslist;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view=inflater.inflate(R.layout.fragment_chats, container, false);

        recyclerView=view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        userslist=new ArrayList<>();
        firebaseUser= FirebaseAuth.getInstance().getCurrentUser();

        databaseReference=FirebaseDatabase.getInstance().getReference("Chatlist").child(firebaseUser.getUid());
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                userslist.clear();;
                for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Chatlist chatlist = snapshot.getValue(Chatlist.class);
                    userslist.add(chatlist);
                }
                chatList();
            }

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

            }
        });

        return view;
    }

    private void chatList() {

        musers=new ArrayList<>();
        databaseReference=FirebaseDatabase.getInstance().getReference("Users");
        databaseReference.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:userslist){
                        if(user.getId().equals(chatlist.getId())){
                            musers.add(user);
                        }
                    }

                }
                userAdapter=new UserAdapter(getContext(), musers,true);
                recyclerView.setAdapter(userAdapter);
            }

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

            }
        });
    }


}

0 个答案:

没有答案