Reputation: 1726
I want to open WhatsApp
to a specific conversation and populate the text field with some string.
Code that I have and I managed to open the conversation with a contact:
private void openConversationWithWhatsapp(String e164PhoneNumber){
String whatsappId = e164PhoneNumber+"@s.whatsapp.net";
Uri uri = Uri.parse("smsto:" + whatsappId);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_TEXT, "text");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TITLE, "title");
intent.putExtra(Intent.EXTRA_EMAIL, "email");
intent.putExtra("sms_body", "The text goes here");
intent.putExtra("text","asd");
intent.putExtra("body","body");
intent.putExtra("subject","subjhect");
startActivity(intent);
}
The text box however is not filled with content. I tried to peek inside the AndroidManifest.xml
file and found the following info regarding their conversation activity:
<activity android:theme="@style/Theme.App.CondensedActionBar" android:name="com.whatsapp.Conversation" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="stateUnchanged">
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
</intent-filter>
</activity>
Does somebody know the extra to use? Are they blocking this on purpose? I saw an API for iOS that they have in their FAQ page.
Upvotes: 14
Views: 22246
Reputation: 1
try {
PackageManager packageManager = getApplicationContext().getPackageManager();
String url = "https://api.whatsapp.com/send?phone=" + number + "&text=" + URLEncoder.encode(message, "UTF-8");
Intent whatappIntent = new Intent(Intent.ACTION_VIEW);
whatappIntent.setPackage("com.whatsapp");
whatappIntent.setData(Uri.parse(url));
whatappIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (whatappIntent.resolveActivity(packageManager) != null) {
startActivity(whatappIntent);
} else {
Log.d("msg","WhatsApp Not installed")
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 729
public void sendMessageOnWhatsApp(String number, String text, Context context){
String url = "https://api.whatsapp.com/send?phone="+number+"&text="+text;
Intent whatsappIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toaster.showMessageShort(context.getString(R.string.no_whatsapp_msg));
}
}
Upvotes: 2
Reputation: 2938
Method 1 - Using android component name
public static void openWhatsAppConversation(Context context, String number, String message) {
number = number.replace(" ", "").replace("+", "");
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(number) + "@s.whatsapp.net");
context.startActivity(sendIntent);
}
Method 2 - Using whatsapp api uri
public static void openWhatsAppConversationUsingUri(Context context, String numberWithCountryCode, String message) {
Uri uri = Uri.parse("https://api.whatsapp.com/send?phone=" + numberWithCountryCode + "&text=" + message);
Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(sendIntent);
}
Upvotes: 2
Reputation: 91
Use Below code to Open conversations in what's App
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage("com.whatsapp");
intent.setData(Uri.parse(String.format("https://api.whatsapp.com/send?phone=%s", "91xxxxxxxxxx")));
if (getPackageManager().resolveActivity(intent, 0) != null) {
startActivity(intent);
} else {
Toast.makeText(this, "Please install whatsApp", Toast.LENGTH_SHORT).show();
}
Note : always enter the country code without "+" to the number.
Upvotes: 2
Reputation: 708
this code is working for me . where the jid is the phone number without '+' and this supports MODIFIED Versions of WhatsApp like WhatsApp+ etc.. only by passing the WhatsApp Package Name . you can find full Code in MyApp
private void openChat(String text, String packageName) {
if (text.trim().isEmpty())
Toast.makeText(context, "Please Enter Number", Toast.LENGTH_SHORT).show();
else {
try {
String smsNumber = Util.getNumber(text) + "@s.whatsapp.net";
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("jid", smsNumber);
i.setPackage(packageName);
context.startActivity(i);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "Make Sure you have WhatsApp App Installed on Your Device", Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 0
Reputation: 101
Update in 2017
based on this Whatsapp FAQ
im using this url
https://api.whatsapp.com/send?phone=62xxxxxxx&text=Hello,%20from%20my%20Apps
which 62 is Indonesia's Dialing code
then i use intent like this (im using kotlin)
val uri = Uri.parse("https://api.whatsapp.com/send?phone=62xxxxxxx&text=Hello,%20from%20my%20Apps")
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
replace 62xxxxx with your number this work fine to me
Upvotes: 10
Reputation: 809
I have done it!
private void openWhatsApp() {
String smsNumber = "7****"; //without '+'
try {
Intent sendIntent = new Intent("android.intent.action.MAIN");
//sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
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"); //phone number without "+" prefix
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
} catch(Exception e) {
Toast.makeText(this, "Error/n" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
Upvotes: 9
Reputation: 903
Try using the following solution to send image and text.
You can setType as "text" and remove extra_stream to use it only for sending text.
Intent sendIntent = new Intent("android.intent.action.SEND");
File f=new File("path to the file");
Uri uri = Uri.fromFile(f);
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.ContactPicker"));
sendIntent.setType("image");
sendIntent.putExtra(Intent.EXTRA_STREAM,uri);
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("919xxxxxxxxx")+"@s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT,"sample text you want to send along with the image");
startActivity(sendIntent);
EXTRA INFORMATION ABOUT THE PROCESS OF FINDING THE SOLUTION :
After reverse engineering WhatsApp, I came across the following snippet of Android manifest,
Normal Share intent, uses "SEND" which does not allow you to send to a specific contact and requires a contact picker.
The specific contact is picked up by Conversation class and uses "SEND_TO" action, but it uses sms body and can not take up image and other attachments.
<activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode" android:name="com.whatsapp.Conversation" android:theme="@style/Theme.App.CondensedActionBar" android:windowSoftInputMode="stateUnchanged">
<intent-filter>
<action android:name="android.intent.action.SENDTO"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="sms"/>
<data android:scheme="smsto"/>
</intent-filter>
</activity>
Digging further, I came across this,
<activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode" android:name="com.whatsapp.ContactPicker" android:theme="@style/Theme.App.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.PICK"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="com.whatsapp"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="audio/*"/>
<data android:mimeType="video/*"/>
<data android:mimeType="image/*"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="text/x-vcard"/>
<data android:mimeType="application/pdf"/>
<data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document"/>
<data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
<data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation"/>
<data android:mimeType="application/msword"/>
<data android:mimeType="application/vnd.ms-excel"/>
<data android:mimeType="application/vnd.ms-powerpoint"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="audio/*"/>
<data android:mimeType="video/*"/>
<data android:mimeType="image/*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:host="send" android:scheme="whatsapp"/>
</intent-filter>
<meta-data android:name="android.service.chooser.chooser_target_service" android:value=".ContactChooserTargetService"/>
</activity>
Finally using a decompiler for ContactPicker and Conversation class,the extra key-value for phone number was found to be "jid".
Upvotes: 5
Reputation: 12745
Sadly, they didn't implement this feature on android (maybe they do in the future).
I already asked a similar question, but with no results. While whatsapp reacts to some intents like the one you show in your code, they simply don't use the text you send. I suppose this is because of safety reasons, imagine the amount of whatsapp-spammer apps in the play store... I don't know how (and why) it is implemented in iOS...
The two alternatives that come close to your solution are
Upvotes: 9
Reputation: 1968
You can use the following snippet for WhatsApp:
public void onClickWhatsApp(View view) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
Upvotes: 0