├── whatsappcontact ├── .npmignore ├── src │ └── main │ │ ├── res │ │ └── values │ │ │ └── strings.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── daffodilsw │ │ └── whatsappcontact │ │ ├── RCTWhatsAppPackage.java │ │ └── WhatsAppContact.java ├── build.gradle └── proguard-rules.pro ├── .gitignore ├── package.json └── README.md /whatsappcontact/.npmignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | whatsappcontact/build/ 3 | .gitignore~ 4 | -------------------------------------------------------------------------------- /whatsappcontact/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Whatsapp Contact 3 | 4 | -------------------------------------------------------------------------------- /whatsappcontact/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /whatsappcontact/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | minSdkVersion 17 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile 'com.facebook.react:react-native:0.12.+' 23 | } 24 | -------------------------------------------------------------------------------- /whatsappcontact/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/daffodil/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-whatsapp-android", 3 | "repository": { 4 | "type": "git", 5 | "url": "git+https://github.com/vikram-sewadra/react-native-whatsapp-contact.git" 6 | }, 7 | "version": "1.0.0", 8 | "description": "React native module to get whatsapp contact from mobile in android", 9 | "main": "index.js", 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "author": { 14 | "name": "vikram singh", 15 | "email": "vikramsewadra@gmail.com" 16 | }, 17 | "license": "ISC", 18 | "readme": "README.md", 19 | "readmeFilename": "README.md", 20 | "_id": "react-native-whatsapp-android@1.0.0", 21 | "_shasum": "49fa9d6f633301a0e3f7592bfa31d5b658fc96e9", 22 | "_from": "../react-native-whatsapp-android", 23 | "_resolved": "file:../react-native-whatsapp-android" 24 | } 25 | -------------------------------------------------------------------------------- /whatsappcontact/src/main/java/com/daffodilsw/whatsappcontact/RCTWhatsAppPackage.java: -------------------------------------------------------------------------------- 1 | package com.daffodilsw.whatsappcontact; 2 | 3 | import com.facebook.react.ReactPackage; 4 | import com.facebook.react.bridge.JavaScriptModule; 5 | import com.facebook.react.bridge.NativeModule; 6 | import com.facebook.react.bridge.ReactApplicationContext; 7 | import com.facebook.react.uimanager.ViewManager; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | import java.util.List; 12 | 13 | /** 14 | * RCT Package for WhatsApp Contact Module 15 | */ 16 | public class RCTWhatsAppPackage implements ReactPackage { 17 | @Override 18 | public List createNativeModules(ReactApplicationContext reactContext) { 19 | List list = new ArrayList<>(); 20 | list.add(new WhatsAppContact(reactContext)); 21 | return list; 22 | } 23 | 24 | @Override 25 | public List> createJSModules() { 26 | return Collections.emptyList(); 27 | } 28 | 29 | @Override 30 | public List createViewManagers(ReactApplicationContext reactContext) { 31 | return Collections.emptyList(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Native WhatsApp Contacts 2 | React Native Package to get WhatsApp Contact for Android. 3 | #### Status 4 | * Preliminary Android support 5 | * API subject to revision, changelog in release notes 6 | 7 | | Feature | iOS | Android | 8 | | ------- | --- | ------- | 9 | | `getAllWhatsAppContacts` | 😞 | ✔ | 10 | 11 | 12 | 13 | ## API 14 | `getAllWhatsAppContacts` (callback) - returns *all* WhatsAppContact contacts as an array of objects 15 | 16 | ## Usage 17 | `getAllWhatsAppContacts` is a database intensive process, and can take a long time to complete depending on the size of the contacts list. Because of this, it is recommended you access the `getAllWhatsAppContacts` method before it is needed, and cache the results for future use. 18 | 19 | Also there is a lot of room for performance enhancements. 20 | 21 | ```js 22 | import { 23 | NativeModules 24 | } from 'react-native' 25 | 26 | NativeModules.WhatsApp.getAllWhatsAppContacts((contacts) => { 27 | console.log(contacts) 28 | }) 29 | ``` 30 | 31 | ## Example Contact Record 32 | ```js 33 | { 34 | phone_no: '0123456789', 35 | name: 'Vikram Singh' 36 | } 37 | ``` 38 | 39 | ## Callback Contact Array of Contact Objects 40 | ```js 41 | [ { phone_no: '0123456789', name: 'Vikram' }, 42 | { phone_no: '0123456789', name: 'Singh Vikram' }, 43 | { phone_no: '0123456789', name: 'Manpreet Kaur' }, 44 | { phone_no: '0123456789', name: 'Munish Kumar' }] 45 | ``` 46 | 47 | ## Getting started 48 | run `npm install react-native-whatsapp-contacts` 49 | 50 | ### Android 51 | * In `android/settings.gradle` 52 | ```gradle 53 | ... 54 | include ':whatsapp-contact' 55 | project(':whatsapp-contact').projectDir=newFile(settingsDir,'../node_modules/react-native-whatsapp-android/whatsappcontact') 56 | 57 | ``` 58 | 59 | * In `android/app/build.gradle` 60 | ```gradle 61 | ... 62 | dependencies { 63 | ... 64 | compile project(':whatsapp-contact') 65 | } 66 | ``` 67 | 68 | * register module (in android/app/src/main/java/[your-app-namespace]/MainActivity.java) 69 | ```java 70 | ... 71 | 72 | import com.daffodilsw.whatsappcontact.RCTWhatsAppPackage; // <--- import module! 73 | 74 | public class MainActivity extends ReactActivity { 75 | ... 76 | 77 | /** 78 | * A list of packages used by the app. If the app uses additional views 79 | * or modules besides the default ones, add more packages here. 80 | */ 81 | @Override 82 | protected List getPackages() { 83 | return Arrays.asList( 84 | new MainReactPackage(), 85 | new RCTWhatsAppPackage() // <--- and add package 86 | ); 87 | } 88 | 89 | ... 90 | } 91 | ``` 92 | 93 | * add Contacts permission (in android/app/src/main/AndroidManifest.xml) 94 | (only add the permissions you need) 95 | ```xml 96 | ... 97 | 98 | 99 | 100 | ... 101 | ``` 102 | 103 | ## Todo 104 | - [ ] android search contact feature 105 | - [ ] implement `getAllWhatsAppContacts` for ios 106 | -------------------------------------------------------------------------------- /whatsappcontact/src/main/java/com/daffodilsw/whatsappcontact/WhatsAppContact.java: -------------------------------------------------------------------------------- 1 | package com.daffodilsw.whatsappcontact; 2 | 3 | import android.content.ContentResolver; 4 | import android.database.Cursor; 5 | import android.provider.ContactsContract; 6 | 7 | import com.facebook.react.bridge.Arguments; 8 | import com.facebook.react.bridge.Callback; 9 | import com.facebook.react.bridge.ReactApplicationContext; 10 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 11 | import com.facebook.react.bridge.ReactMethod; 12 | import com.facebook.react.bridge.WritableArray; 13 | import com.facebook.react.bridge.WritableMap; 14 | 15 | /** 16 | * Whats app contact Java module for RCT Native 17 | */ 18 | public class WhatsAppContact extends ReactContextBaseJavaModule { 19 | 20 | public WhatsAppContact(ReactApplicationContext reactContext) { 21 | super(reactContext); 22 | } 23 | 24 | @Override 25 | public String getName() { 26 | return "WhatsApp"; 27 | } 28 | 29 | /** 30 | * Method for RCT Native to get all WhatsApp Contact 31 | * 32 | * @param callback Callback for function with List of contacts 33 | */ 34 | @ReactMethod 35 | public void getAllWhatsAppContacts(Callback callback) { 36 | ContentResolver cr = getReactApplicationContext().getContentResolver(); 37 | 38 | //RowContacts for filter Account Types 39 | Cursor contactCursor = cr.query( 40 | ContactsContract.RawContacts.CONTENT_URI, 41 | new String[]{ContactsContract.RawContacts._ID, 42 | ContactsContract.RawContacts.CONTACT_ID}, 43 | ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?", 44 | new String[]{"com.whatsapp"}, 45 | null); 46 | 47 | //ArrayList for Store WhatsApp Contact 48 | WritableArray writableArray = Arguments.createArray(); 49 | 50 | if (contactCursor != null && contactCursor.getCount() > 0 && contactCursor.moveToFirst()) { 51 | do { 52 | //WhatsAppContactId for get Number,Name,Id ect... 53 | // from ContactsContract.CommonDataKinds.Phone 54 | String whatsappContactId = contactCursor 55 | .getString(contactCursor.getColumnIndex( 56 | ContactsContract.RawContacts.CONTACT_ID)); 57 | if (whatsappContactId != null) { 58 | //Get Data from ContactsContract.CommonDataKinds.Phone of Specific CONTACT_ID 59 | Cursor whatsAppContactCursor = cr.query( 60 | ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 61 | new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, 62 | ContactsContract.CommonDataKinds.Phone.NUMBER, 63 | ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}, 64 | ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 65 | new String[]{whatsappContactId}, null); 66 | 67 | if (whatsAppContactCursor != null) { 68 | whatsAppContactCursor.moveToFirst(); 69 | String name = whatsAppContactCursor.getString(whatsAppContactCursor. 70 | getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 71 | String number = whatsAppContactCursor.getString(whatsAppContactCursor. 72 | getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 73 | whatsAppContactCursor.close(); 74 | 75 | WhatsAppBean whatsAppBean = new WhatsAppBean(); 76 | whatsAppBean.setName(name); 77 | whatsAppBean.setPhoneNo(number); 78 | 79 | 80 | //Add Number to ArrayList 81 | writableArray.pushMap(whatsAppBean.toMap()); 82 | } 83 | } 84 | } while (contactCursor.moveToNext()); 85 | contactCursor.close(); 86 | 87 | } 88 | 89 | // Return WriteAbleArray of WhatsApp contact list to RCTJs function in callback 90 | callback.invoke(writableArray); 91 | } 92 | 93 | /** 94 | * Data model to hold a Whats app contact 95 | */ 96 | private class WhatsAppBean { 97 | private String name; 98 | private String phoneNo; 99 | 100 | public void setName(String name) { 101 | this.name = name; 102 | } 103 | 104 | public void setPhoneNo(String phoneNo) { 105 | this.phoneNo = phoneNo; 106 | } 107 | 108 | /** 109 | * Convert a WhatsApp contact object to WriteAble Map 110 | * 111 | * @return Writeable map of WhatsappContact of this 112 | */ 113 | public WritableMap toMap() { 114 | WritableMap writableMap = Arguments.createMap(); 115 | writableMap.putString("name", name); 116 | writableMap.putString("phone_no", phoneNo); 117 | return writableMap; 118 | } 119 | } 120 | } 121 | --------------------------------------------------------------------------------