7

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

Community
  • 1
  • 1
Erick Filho
  • 1,962
  • 3
  • 18
  • 31

4 Answers4

2

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")))
            }
        }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

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)
                    )
            );
        }
    }
}
Falah H. Abbas
  • 512
  • 5
  • 11
  • 1
    While this code snippet may be the solution, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion – Narendra Jadhav Aug 11 '18 at 10:25
1

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)
}
Morgan Koh
  • 2,297
  • 24
  • 24
1

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)));
}
Yury Matatov
  • 805
  • 3
  • 11
  • 23