8
votes

after long Googleing which didn't brought the result I hoped it would, I have two questions about accessing WhatsApp from another Android app.

First of all I want to explain my current development status:

Wrote an app with which you can share some text via WhatsApp. The app is exactly doing what it is supposed to do (as I am completely new to Android development). The first way I found was described in WhatsApp's "FAQ for Android developers". It creates a new intent, prefills the text which should be send and opens the contact picker:

int pos = 0; //0 is just an example value
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
PushAlert pa = pushAlerts.get(pos); //get my text object from ArrayList
sendIntent.setPackage("com.whatsapp"); //directly choose WhatsApp as sharing app
sendIntent.putExtra(Intent.EXTRA_TEXT, "*" + pa.getTitle() + " * \n +" + pa.getContent()); //filling 
sendIntent.setType("text/plain");
startActivity(sendIntent); //Open contact picker

Googled and googled so I found a way (code snippet) to open a specific personal chat and prefill it with the text I want to share:

private void openWhatsAppChat(){
  Intent sendIntent = new Intent("android.intent.action.SEND");
  sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.ContactPicker"));
  sendIntent.setType("text");
  sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("phone number")+"@s.whatsapp.net"); //number without '+' prefix and without '0' after country code
  sendIntent.putExtra(Intent.EXTRA_TEXT,"sample text you want to send along with the image");
  startActivity(sendIntent);
}

So my questions are:

  1. How can I get the WhatsApp ID of a WhatsApp group?
  2. Can I open the group chat and paste my text just with replacing the phone number in method 2 by the group ID? Or is there another way to open and prefill a group chat?
1

1 Answers

6
votes

You have to use group link.
When the user install your app, you should ask them to copy the group link from the whatsapp group info, then you store it to access that group directly from your app.
This link is only visible by groups admins, so if the user isn't admin, you should instruct them to ask the link from the admin.
Although this link was intended by whatsapp for invitation to groups, it makes the job of opening the desired group chat.

Intent intentWhatsapp = new Intent(Intent.ACTION_VIEW);
String url = "https://chat.whatsapp.com/<group_link>";
intentWhatsapp.setData(Uri.parse(url));
intentWhatsapp.setPackage("com.whatsapp");
startActivity(intentWhatsapp);