5

I've read how to open whatsapp from my app, as explained from the question How to open Whatsapp from other app - but it does not explain there how to open chat with a specific contact. Is there any way to open chat with specific contacts?

Niall Cosgrove
  • 1,273
  • 1
  • 15
  • 24
Akhmadi Blank
  • 51
  • 1
  • 2
  • Possible duplicate of [Sending Email in Android using JavaMail API without using the default/built-in app](https://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a) – mquantin Sep 18 '17 at 00:21

2 Answers2

6

You can open whats app with specific contact but that contact must have a whats app account.

private void openWhatsApp() {
    String smsNumber = "91xxxxxxxxxx"; //without '+'
    try {
        Intent sendIntent = new Intent("android.intent.action.MAIN");
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.setType("text/plain");
        sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
        sendIntent.putExtra("jid", smsNumber + "@s.whatsapp.net");
        sendIntent.setPackage("com.whatsapp");
        startActivity(sendIntent);
    } catch(Exception e) {
        Toast.makeText(this, "Error\n" + e.toString(), Toast.LENGTH_SHORT).show();
    }
}

Common mistakes with above codes are

  1. ACTION_SEND not ACTION_SENDTO
  2. Very careful with contact number, It should have "91" or any other code based on the country.
  3. Should not use any special characters like + before the number.
livemaker
  • 464
  • 1
  • 9
  • 19
3
private void sendMsg(){
  String msgurl = "https://api.whatsapp.com/send?phone=+9199999999&text=Hello";
  Intent i = new Intent(Intent.ACTION_VIEW);
  i.setData(Uri.parse(msgurl));
  startActivity(i);
  }
  • my self i was Lookig for this answer, Swift Version ```"whatsapp://send?phone=24990.....&abid=12354&text=Hello"``` – mazenqp Mar 06 '22 at 07:16