11

Is it possible to send message to a specific contact through whatsapp directly from another app? I know the contact ID. I don't want to open whatsapp via Intent. I just want to send the message directly like normal sms.

i have tried other solutions posted on stackoverflow but they are not working for me.

Naddy
  • 2,624
  • 6
  • 24
  • 38
  • 2
    Have you found any solutions? – Mehul Joisar Nov 15 '13 at 08:10
  • @MehulJoisar Currently I guess using Intent is the only option. – Naddy Nov 15 '13 at 12:07
  • @MahiSingh No solution found related to this question. – Naddy Nov 16 '13 at 12:26
  • @Naddy can u give me some link or suggestion how to get contact id – Mahi Nov 16 '13 at 12:56
  • 1
    @MahiSingh contact id as in what?? All of the Whatsapp id is in the form of `PhoneNumber@s.whatsapp.net`. – Naddy Nov 16 '13 at 14:17
  • @Naddy how to get programmatically whats up id.I saw this question http://stackoverflow.com/questions/19081654/send-to-specific-contact-whatsapp?lq=1 But i dont understand what is getSherlockactivity and how they get whats up id in android – Mahi Nov 16 '13 at 14:37
  • They are not getting whatsapp id programmatically. All they are doing is sending an Intent to whatsapp which opens up **Whatsapp contact picker**. Then you choose the contact, and the text is sent to that specific contact. `getsherlockActivity()` is same as `getActivity()`. It returns your Activity. Only difference is that you use `getSherlockActivity()` when you use ABS library. – Naddy Nov 16 '13 at 15:09
  • @Naddy are you solve this problem. if you solve this problem , could you please share your procedure. Thanks – BeingMIAkashs Dec 17 '13 at 18:40
  • That is unfortunately not possible. Source: http://stackoverflow.com/questions/6284011/start-whatsapp-with-android-content-intent-action-sendto – Jack Miller Jan 07 '14 at 10:52

6 Answers6

9

Let me know if it works for you,

Uri mUri = Uri.parse("smsto:+9876543210");
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("sms_body", "The text goes here");
mIntent.putExtra("chat",true);
startActivity(mIntent);
Mehul Joisar
  • 15,218
  • 6
  • 47
  • 56
  • 13
    It simply opens the chat view of that particular id.The message is not sent. – Naddy Nov 15 '13 at 13:05
  • 1
    there is some problem with `mIntent.putExtra("sms_body", "The text goes here");` this is failing in setting the text body. – Srujan Barai Nov 14 '15 at 17:40
  • 2
    Does it work when number isn't saved or never had chat with this contact? – Anshul Tyagi Mar 10 '16 at 06:59
  • java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contact/com.example.contact.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO dat="" pkg=com.whatsapp (has extras) } – Prakash Gajera Jul 08 '16 at 07:11
3

It wont send message in a go but lets you open the screen for the same in whatsapp:

private void openWhatsApp() {
    String smsNumber = "91XXXXXXXX20";
    boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
    if (isWhatsappInstalled) {

        Intent sendIntent = new Intent("android.intent.action.MAIN");
        sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
        sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(smsNumber) + "@s.whatsapp.net");//phone number without "+" prefix

        startActivity(sendIntent);
    } else {
        Uri uri = Uri.parse("market://details?id=com.whatsapp");
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        Toast.makeText(this, "WhatsApp not Installed",
                Toast.LENGTH_SHORT).show();
        startActivity(goToMarket);
    }
}

private boolean whatsappInstalledOrNot(String uri) {
    PackageManager pm = getPackageManager();
    boolean app_installed = false;
    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    } catch (PackageManager.NameNotFoundException e) {
        app_installed = false;
    }
    return app_installed;
}
Prakhar Kulshreshtha
  • 1,213
  • 11
  • 21
1

Please try this. Its working perfectly fine for me.

    Intent sendIntent = new Intent("android.intent.action.MAIN");
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setPackage("com.whatsapp");
    sendIntent.setType("text/plain");
    sendIntent.putExtra("jid", "9194******22" + "@s.whatsapp.net");// here 91 is country code
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Demo test message");
    startActivity(sendIntent);
Bansari
  • 11
  • 2
1

This will be work but you need to mobile no. with country code like for India 91. eg. 91758XXXXXX2

String url = "https://api.whatsapp.com/send?phone=" + 91758XXXXXX2 + "&text=" + URLEncoder.encode("good morning", "UTF-8");
    i.setPackage("com.whatsapp");
    i.setData(Uri.parse(url));
    if (i.resolveActivity(packageManager) != null) {
      startActivity(i);
    }
0

Please try this,

public void onClickWhatsApp(View view) {

Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
        String text = "YOUR TEXT HERE";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
    waIntent.putExtra(Intent.EXTRA_TEXT, text);//
    startActivity(Intent.createChooser(waIntent, "Share with"));
} else {
    Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
            .show();
}
}

Source : Please check this answer for further details

Community
  • 1
  • 1
0

I hope this code helps you

String text = "This is a test";// Replace with your message.

String toNumber = "xxxxxxxxxx"; // Replace with mobile phone number without +Sign or leading zeros, but with country code
//Suppose your country is India and your phone number is “xxxxxxxxxx”, then you need to send “91xxxxxxxxxx”.


Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://api.whatsapp.com/send?phone="+toNumber +"&text="+text));
startActivity(intent);
mohsen
  • 812
  • 12
  • 21