Firebase: Android : How to show list of contacts who have the app Installed

Viewed 4498

I am using Google auth via firebase and am logging the users in successfully. I also have retrieved the list of contacts from the phonebook (device) and displaying it on a listview in a fragment in my app. But now I wish to show the users amongst my contacts who have my app installed, so that when clicked on they will go to the private chat with them, the other contacts, when clicked on will enable them to send an app invite. In a nutshell: I want to view the list of contacts who have the app installed on their device.

5 Answers

I was able to achieve this in three straightforward steps.

  • Get a list of your phone contacts
  • Get a list of all the phone numbers on Firestore
  • Compare the two lists and return common elements.

In order to use my approach, you need to have a collection on Firestore that has the phone number of all your users as documents just like the image below:

enter image description here

Here are the steps:

Step 1: I got a list of all the user's contacts by using ContentResolver. You can use the method below to retrieve this list provided you have the READ_CONTACTS permission granted.

public ArrayList<String> getContacts(ContentResolver cr) {

    // list to be returned
    ArrayList<String> numbers = new ArrayList<>();

    // cursor
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    if ((cur != null ? cur.getCount() : 0) > 0) {

        while (cur != null && cur.moveToNext()) {

            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {

                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.i(TAG, "Name: " + name);
                    numbers.add(formatRightWay(phoneNo));
                }

                pCur.close();
            }
          }
        }

        if(cur!=null){
            cur.close();
          }

        return numbers;
}

Step 2: I got a list of all the phone numbers on Firestore by fetching the document IDs of the user collection. Here's a quick implementation:

firestore.collection("users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
    if (task.isSuccessful()) {
        List<String> list = new ArrayList<>();
        for (QueryDocumentSnapshot document : task.getResult()) {
            list.add(document.getId());
        }
        // this is the list you need
        Log.d(TAG, list.toString());
    } else {
        Log.d(TAG, "Error getting documents: ", task.getException());
    }
  }
});

Step 3: Write a method that compares the two lists and returns similar elements.

public static ArrayList<String> shuffleBothLists(ArrayList<String> phoneContacts, List<String> firebaseContacts) {
    ArrayList<String> result = new ArrayList<>();

    for(String s: firebaseContacts) {
        if(phoneContacts.contains(s) && !result.contains(s)) {
            result.add(s);
        }
    }

    return result;
}

The list returned by the method above are your contacts that have the app installed.

Cheers!

Related