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
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
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)
)
);
}
}
}
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)));
}
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)
}
this Kotlin code works fine for me