Reputation: 3681
I need to send a message from my app to a selected contact using WhatsApp. I have the contact name, number, ID and the message I want to send. just need to be able to send it without having to pick the contact in WhatsApp or the app to share the message. I´ve tested several approaches but I have´nt found any working solution, the simplest is as follows:
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", "message");
i.setPackage("com.whatsapp");
startActivity(i);
With this approach the contact is selected automatically but then pops out that contact´s conversation inside WhatsApp and the worst part is that the message isn´t even there. With the message there I would only have to hit send and would be something, even when I need to do it all at once.
Thanks in advance!
PD: Thanks for the answer being root, but thats my last option, I just need to be able to send the message from a non rooted device.
Upvotes: 0
Views: 3536
Reputation: 11873
Frankly speaking there's no public API available at this moment to share message for a specific contact. However if you aren't concerned with rooted device then you can follow the steps,
protected void whatsAppSendMessage(String[] paramArrayOfString, String paramString) {
try {
Shell shell = Shell.startRootShell();
int j = paramArrayOfString.length;
for (int i = 0; i < j; i++) {
String str3;
long l1;
long l2;
int k;
String str1;
String str2;
Random localRandom = new Random(20L);
Log.d("AUTO",
"ps | grep -w 'com.whatsapp' | awk '{print $2}' | xargs kill");
commandsTestOnClick("ps | grep -w 'com.whatsapp' | awk '{print $2}' | xargs kill");
str3 = paramArrayOfString[i] + "@s.whatsapp.net";
l1 = System.currentTimeMillis();
l2 = l1 / 1000L;
k = localRandom.nextInt();
str1 = "sqlite3 /data/data/com.whatsapp/databases/msgstore.db \"INSERT INTO messages (key_remote_jid, key_from_me, key_id, status, needs_push, data, timestamp, MEDIA_URL, media_mime_type, media_wa_type, MEDIA_SIZE, media_name , latitude, longitude, thumb_image, remote_resource, received_timestamp, send_timestamp, receipt_server_timestamp, receipt_device_timestamp, raw_data, media_hash, recipient_count, media_duration, origin)VALUES ('"
+ str3
+ "', 1,'"
+ l2
+ "-"
+ k
+ "', 0,0, '"
+ paramString
+ "',"
+ l1
+ ",'','', '0', 0,'', 0.0,0.0,'','',"
+ l1
+ ", -1, -1, -1,0 ,'',0,0,0); \"";
str2 = "sqlite3 /data/data/com.whatsapp/databases/msgstore.db \"insert into chat_list (key_remote_jid) select '"
+ str3
+ "' where not exists (select 1 from chat_list where key_remote_jid='"
+ str3 + "');\"";
str3 = "sqlite3 /data/data/com.whatsapp/databases/msgstore.db \"update chat_list set message_table_id = (select max(messages._id) from messages) where chat_list.key_remote_jid='"
+ str3 + "';\"";
Log.d("AUTO", str1);
Log.d("AUTO", str2);
Log.d("AUTO", str3);
shell.add(
new SimpleCommand(
"chmod 777 /data/data/com.whatsapp/databases/msgstore.db"))
.waitForFinish();
shell.add(new SimpleCommand(str1)).waitForFinish();
shell.add(new SimpleCommand(str2)).waitForFinish();
shell.add(new SimpleCommand(str3)).waitForFinish();
}
shell.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Tested and works perfectly even without opening the WhatsApp!
Original source,
Whatsapp message share without opening whatsapp
Upvotes: 1
Reputation: 15414
You cannot do that as of now, because WhatsApp have not opened their APIs to the developers. The best you can do is as described here.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Its strange, because apparently you can select the contact in iOS by passing some extra params.
Upvotes: 1