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,664
  • 6
  • 25
  • 38

5 Answers5

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,348
  • 6
  • 48
  • 57
  • 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
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
  • 1,065
  • 16
  • 24