0

I searched various answers but all of them are outdated. Whenever I tried using send to it only opens contact chooser.

Sanwal Singh
  • 1,765
  • 3
  • 17
  • 35
  • 1
    Welcome to SO. Please have a look at [how-to-ask](http://stackoverflow.com/help/how-to-ask) – swe Nov 23 '16 at 10:14

4 Answers4

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

 startActivity(sendIntent);

Update:

The aforementioned hack cannot be used to add any particular message, so here is the new approach. Pass the user mobile in international format here without any brackets, dashes or plus sign. Example: If the user is of India and his mobile number is 94xxxxxxxx , then international format will be 9194xxxxxxxx. Don't miss appending country code as a prefix in mobile number.

  private fun sendMsg(mobile: String, msg: String){
    try {
        val packageManager = requireContext().packageManager
        val i = Intent(Intent.ACTION_VIEW)
        val url =
            "https://wa.me/$mobile" + "?text=" + URLEncoder.encode(msg, "utf-8")
        i.setPackage("com.whatsapp")
        i.data = Uri.parse(url)
        if (i.resolveActivity(packageManager) != null) {
            requireContext().startActivity(i)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

Note: This approach works only with contacts added in user's Whatsapp account.

Rishabh Maurya
  • 1,448
  • 3
  • 22
  • 40
1

You have to set the package name in the Intent like this

intent.setPackage("com.whatsapp");

Full example:

Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", smsText);  
i.setPackage("com.whatsapp");  
startActivity(i);
Sanif SS
  • 602
  • 5
  • 18
1

You just fire the below intent on the click on button:

Uri mUri = Uri.parse("smsto:" + mobile1);
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(Intent.createChooser(mIntent, ""));

if number available on whatsapp then that particular user chat open and you send your message.If number not available on whatsapp the alert diaolge open in that case.

hope this help you ;-)

Sarbjyot
  • 136
  • 2
  • 8
1

Here is solution

 private void openWhatsApp(String number) {
    String whatsAppMessage = "Hello!";
    Uri uri = Uri.parse("smsto:" + number);
    Intent i = new Intent(Intent.ACTION_SENDTO, uri);
    i.setPackage("com.whatsapp");
    startActivity(i);
   }

Call above function and pass number, by which by want to open chat in Whatsapp messenger.

Hope it will work for you. :)

Sanwal Singh
  • 1,765
  • 3
  • 17
  • 35