Naddy
Naddy

Reputation: 2674

Send message to specific contact through whatsapp from another app

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.

Upvotes: 11

Views: 22821

Answers (5)

mohsen
mohsen

Reputation: 1235

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);

Upvotes: 0

Bharat Ghorpade
Bharat Ghorpade

Reputation: 11

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);
    }

Upvotes: 1

Bansari
Bansari

Reputation: 11

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);

Upvotes: 1

user1733583
user1733583

Reputation:

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

Upvotes: 0

Mehul Joisar
Mehul Joisar

Reputation: 15358

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);

Upvotes: 9

Related Questions