Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Whatsapp from other app with a specific contact?

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?

like image 658
Akhmadi Blank Avatar asked Sep 17 '17 17:09

Akhmadi Blank


People also ask

How can I access WhatsApp chat from other app?

Launch WhatsApp on the target cell phone and tap on "WhatsApp Web" by navigating to the "Settings" menu. Step 3. From the phone, scan the QR code and the connection will be established automatically after which you can read WhatsApp messages from another device.


2 Answers

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.
like image 191
livemaker Avatar answered Nov 14 '22 23:11

livemaker


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);
  }
like image 41
Abhishek Prajapati Avatar answered Nov 14 '22 23:11

Abhishek Prajapati