Is a friends feature required for developing a chat app with phone authentication

I've seen a lot of chat application examples on youtube, google and github on how to make chat applications. I have used the same logic and made an application but not able to recieve messaged from the other user. Like it's getting saved in the database but not able to retrieve it. It just striked my mind that almost all of the examples i saw have this feature of sending and accepting friend request. is that compulsory for making an app using firebase. Like can that be the reason as to why i'm not able to retrieve the messages sent by the other user?

Please help me out i've been stuck at this point for months now

If you want to see how i retrieve messages... Here's the code for it

private void fetchMessages() {
    rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            ListView listView = (ListView) findViewById(R.id.messageList);
            ArrayAdapter<String> adapter;
            adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, messagesList);
            String message = dataSnapshot.getValue().toString();
            message = dataSnapshot.child("message").getValue().toString();
//                Log.d("TAG",message );
            messagesList.add(message);
            adapter.notifyDataSetChanged();
            listView.setAdapter(adapter);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

Database

{
"Messages" : {
"8hbPUUlw39OABqmqSJ63BAHFjan2" : {
  "OcAXeb0KAzfkXSNXyDddAOpBdyh1" : {
    "-L7tuwuIKTkaskiJxU0c" : {
      "message" : "Hii",
      "seen" : false,
      "time" : 1521390178351,
      "type" : "text"
    },
    "-L7u3Z8fnmwKpyvf-0Qy" : {
      "message" : "hi",
      "seen" : false,
      "time" : 1521392698442,
      "type" : "text"
    },
    "-L7u3oT5nGW1181Hfh0g" : {
      "message" : "Ufff",
      "seen" : false,
      "time" : 1521392765184,
      "type" : "text"
    },
    "-L7uFBt6kECyA7FvUmhm" : {
      "message" : "hhfjgivugihihjihycvyuhuhuhi",
      "seen" : false,
      "time" : 1521395748801,
      "type" : "text"
    }
  }
},
"OcAXeb0KAzfkXSNXyDddAOpBdyh1" : {
  "8hbPUUlw39OABqmqSJ63BAHFjan2" : {
    "-L7tuwuIKTkaskiJxU0c" : {
      "message" : "Hii",
      "seen" : false,
      "time" : 1521390178351,
      "type" : "text"
    },
    "-L7u3Z8fnmwKpyvf-0Qy" : {
      "message" : "hi",
      "seen" : false,
      "time" : 1521392698442,
      "type" : "text"
    },
    "-L7u3oT5nGW1181Hfh0g" : {
      "message" : "Ufff",
      "seen" : false,
      "time" : 1521392765184,
      "type" : "text"
    },
    "-L7uFBt6kECyA7FvUmhm" : {
      "message" : "hhfjgivugihihjihycvyuhuhuhi",
      "seen" : false,
      "time" : 1521395748801,
      "type" : "text"
    }
  },
  "OcAXeb0KAzfkXSNXyDddAOpBdyh1" : {
    "-L7u8PbR_UNNxQT93sEt" : {
      "message" : "Hi",
      "seen" : false,
      "time" : 1521393970012,
      "type" : "text"
    }
  }
}
},
"Users" : {
"8hbPUUlw39OABqmqSJ63BAHFjan2" : {
  "Image" : "default",
  "Name" : "Akash",
  "Status" : "Hey",
  "Thumb_image" : "default",
  "Unique" : "8hbPUUlw39OABqmqSJ63BAHFjan2"
},
"OcAXeb0KAzfkXSNXyDddAOpBdyh1" : {
  "Image" : "default",
  "Name" : "Sh",
  "Status" : "Hey",
  "Thumb_image" : "default",
  "Unique" : "OcAXeb0KAzfkXSNXyDddAOpBdyh1"
}
}
}

All users activity

atabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usersRef = rootRef.child("Users");

    ValueEventListener eventListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            ListView listView = (ListView) rootView.findViewById(R.id.usersList);
            ArrayAdapter<String> adapter;
            adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, al);
            for(DataSnapshot ds : dataSnapshot.getChildren()) {

                unique = ds.child("Unique").getValue().toString();
                name = ds.child("Name").getValue().toString();
                list_user_id = ds.getKey();


                al.add(name);



            }
            listView.setAdapter(adapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getActivity(),"Please Check Your Internet Connection",Toast.LENGTH_SHORT).show();
        }
    };
    usersRef.addListenerForSingleValueEvent(eventListener);

     usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            UserDetails.chatWith = al.get(position);
            String user_id = al.get(position);
            Intent intent = new Intent(getActivity(), Chat.class);
            intent.putExtra("visit_user_id",unique);
            intent.putExtra("user_name",name);
            startActivity(intent);
            Log.d("TAG",unique);
        }
    });
    return rootView;

1 answer

  • answered 2018-03-20 19:10 Frank van Puffelen

    The first thing to do is to stop ignoring errors:

    public void onCancelled(DatabaseError databaseError) { 
      throw databaseError,toException(); 
    } 
    

    This may uncover valuable information about why your app isn't working.

    Next up try running the code under a debugger. Does your onChildAdded get triggered? If so, does message get the value you'd expect it to have?

    A third step is to move the code creating the adapter out of onChildAdded. Right now you're creating a new adapter for each message, which means you're going to be showing at most one message.

    ListView listView = (ListView) findViewById(R.id.messageList);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, messagesList);
    listView.setAdapter(adapter);
    
    rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String message = dataSnapshot.child("message").getValue().toString();
            messagesList.add(message);
            adapter.notifyDataSetChanged();
        }
        ...