sandeep
sandeep

Reputation: 89

How to pass a specific text to a specific number through WhatsApp?

I have this code. It is only opening WhatsApp with a particular number but not with text.

Uri uri = Uri.parse("smsto:" +mobilenumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", "Hello StackOverFlow");
i.putExtra("chat",true);
i.setPackage("com.whatsapp");
startActivity(i);

I have tried using Intent.EXTRA_TEXT but no result.

How to pass the text?

Upvotes: 0

Views: 668

Answers (1)

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

You can not directly send message to specific contact in whatsapp from your code. you can pass text to listing screen then user can choose user to send the message.

Try below code:

 final Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
 whatsappIntent.setPackage("com.whatsapp");
 whatsappIntent.putExtra(Intent.EXTRA_TEXT, text);
 whatsappIntent.setType("text/plain");
 try {
       mContext.startActivity(whatsappIntent);
 } catch (ActivityNotFoundException ex) {
       ex.printStackTrace();
       Toast.makeText(mContext, "WhatsApp is not installed.", Toast.LENGTH_SHORT).show();
 }

EDIT:

you can not Send Whatsapp message to specific contact. Refer Send Whatsapp message to specific contact

I hope it helps!

Upvotes: 1

Related Questions