javaandroidwhatsapp

How to open Whatsapp group by clicking a button?


I made a simple phone directory and was able to open the Whatsapp contacts by clicking a button.

Here is the sample code:

Intent intentWhatsapp = new Intent("android.intent.action.MAIN");
intentWhatsapp.setAction(Intent.ACTION_VIEW);
String url = "https://api.whatsapp.com/send?phone=" + "90xxxxxxxx";
intentWhatsapp.setData(Uri.parse(url));
intentWhatsapp.setPackage("com.whatsapp");
startActivity(intentWhatsapp);}

How can I open the Whatsapp group by clicking a button in android?

Hope to give me any suggestions!


Solution

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