Skip to content Skip to sidebar Skip to footer

Displaying Messages From Firebase Database

I'm creating a social messaging app. the ideal on how the app function is where the messages are saved to the firebase database and then displayed on the RecyclerView.. the messa

Solution 1:

hi i am fatching my massages from firebase database with this, try this it will help you :-

private void readMessage(final String myId, final String userId) {
        mChat = new ArrayList<>();
        reference = FirebaseDatabase.getInstance().getReference(chatTableName);
        try {
            reference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    mChat.clear();
                    Chat chat = null;
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        chat = snapshot.getValue(Chat.class);
                        assert chat != null;
                        if (chat.getReceiver().equals(myId) && chat.getSender().equals(userId) ||
                                chat.getReceiver().equals(userId) && chat.getSender().equals(myId)) {
                            mChat.add(chat);
                        }
                        adapter = new MessageAdapter(UserMessageActivity.this, mChat);
                        recyclerView.setAdapter(adapter);
                    }
                }

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

                }
            });
        } catch (Exception e) {
            Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

Post a Comment for "Displaying Messages From Firebase Database"