2

What approach is Whatsapp using to show an "Invite" button for contacts which are not on Whatsapp?

I also want to show a button in Contact list view for only those which don't exist in my app in Firebase database.

I am using Custom BaseAdapter to show contacts in List View. Can you please help me in understanding how Whatsapp Contact is working?

My Question is not duplicate as that is to read contact list only. I want to show 'Invite' button also as per firebase data.

Thanks!

Hitesh
  • 31
  • 8

2 Answers2

0

Well I am not confident what exactly whatsapp is using but as I can the contacts that are not on whatsapp appear in the last. So you can dump the contact list from your firebase Db then query the contacts in the device and make two arraylist in which one is having contact list present in your app means your app users then another is the new users then merge them and show in the Recyclerview or Listview as per your requirement and for invite button you can use different cell layout which contais a button by setting any bool in array list and check it in your base adapter or you can also set its visibility in the same cell as per data in your arraylist.

Umar Ata
  • 4,170
  • 3
  • 23
  • 35
0

First of All you have to get the all contacts from clients device,

Note : You Have to Check For Contacts Permissions by your self & don't Forgot to add Permissions Manifest.

Call initData() in onCreate or After Checking Permissions.

here is the code to get Contacts from Clients Device.

private void initData() {
    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,null);
    while(Objects.requireNonNull(cursor).moveToNext()){
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        // Finds the contact in our database through the Firebase Query to know whether that contact is using our app or not.
        findUsers(number);
    }
}

While Getting Each Contact one by one from clients device, simultaneously we will trigger the firebase query to check whether that contact is using our app or not.

So we are using "findUser" Method to check whether that contact is using our app or not.

private void findUsers(final String number){
    Query query = FirebaseDatabase.getInstance().getReference()
            .child("User")
            .orderByChild("phone")
            .equalTo(number);

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if (snapshot.getValue() != null){
                Map<String, Object> map = (Map<String, Object>) snapshot.getValue();
                // this will print whole map of contacts who is using our app from clients contacts.
                Log.d("ContactSync", snapshot.getValue().toString());
                // so you can use any value from map to add it in your Recycler View.
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Log.d("ContactSync", error.getMessage());
        }
    });
}

Here is how my database structure looks like. database structure image

Thanks for reading this answer, I hope this will be helpful!

Kamal Panara
  • 482
  • 7
  • 16