How to open Viber public chat from Android app

5.4k views Asked by At

I know how to open a conversation with a number in Viber: How to start Viber call from an Android app [new version]?

But how do I open a public chat? Any ideas?

Thanks in advance

4

There are 4 answers

0
Stanislav Tkach On

this Kotlin code works fine for me

        val viberPackageName = "com.viber.voip"
        val phone= "5757575757"

        try {
            activity?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("viber://add?number=$phone")))
        } catch (ex: ActivityNotFoundException) {
            try {
                activity?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$viberPackageName")))
            } catch (ex: ActivityNotFoundException) {
                activity?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$viberPackageName")))
            }
        }
1
Falah H. Abbas On

Java code

  public void addViberNumber(Context context,String phone) {
    String viberPackageName = "com.viber.voip";

    try {
        context.startActivity(new
                        Intent(Intent.ACTION_VIEW,
                        Uri.parse("viber://add?number="+phone)
                )
        );
    } catch (ActivityNotFoundException ex) {
        try {
            context.startActivity
                    (new Intent(Intent.ACTION_VIEW,
                            Uri.parse("market://details?id=+" + viberPackageName))
                    );
        } catch (ActivityNotFoundException exe) {
            context.startActivity(new Intent(Intent.ACTION_VIEW,
                            Uri.parse("https://play.google.com/store/apps/details?id=" + viberPackageName)
                    )
            );
        }
    }
}
0
Yury Matatov On

For opening Viber public chat, you need to use the full contact's phone number, and change char + to %2B.

For example:

+972526461150 - is a full phone number with country code and region

%2B972526461150 - this is what the phone number should look like for Viber.

Code:

private void sendViaViber() {
    String textMessage =  "Hello my friend. How are you?";
    String phoneNumber = "%2B972526461150"
   
    String url = "viber://chat?number=" + phoneNumber + "&draft=" + textMessage;
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
0
Morgan Koh On

I'm using com.google.i18n.phonenumbers.PhoneNumber as the model that I passed it, but the functionality is the same.

For Viber, you'll need the countryCode and the nationalNumber as a String format and then you passed it into the Uri, with the Intent URI that is specified by Viber.

Then, you just launch the Intent.

private fun launchViberChat(phoneNumber: Phonenumber.PhoneNumber) {
    val formatString = "${phoneNumber.countryCode}${phoneNumber.nationalNumber}"
    val intent = Intent(
        Intent.ACTION_VIEW,
        Uri.parse("viber://add?number=$formatString")
    ).apply {
        setPackage("com.viber.voip")
    }
    startActivity(intent)
}