How to open chat activity of specific Viber contact using phone number to directly send a message?

For example, I have a phone number and a button called "Chat with contact". When clicked on button my app should open Viber activity that represents specific Viber contact chat window based on the phone number. I have something like this, but startActivity() method throws ActivityNotFoundException.

        val intent = Intent(Intent.ACTION_SENDTO).apply {
            `package` = "com.viber.voip"
            data = Uri.parse("sms:$phoneNumber")
            putExtra("address", phoneNumber)
        }
        startActivity(intent)

I know that Viber provided that action before, but then the action has been hidden in the manifest file maybe 3 or 4 years ago. Is there any idea how can I do that nowadays?

Upvotes: 0

Views: 801

Answers (1)

fkdplc
fkdplc

Reputation: 31

I just analyzed Viber's Manifest and saw that:

<intent-filter>
    <action
        android:name="android.intent.action.SEND" />
    <category
        android:name="android.intent.category.DEFAULT" />
    <data
        android:scheme="smsto" />
    <data
        android:scheme="sms" />
</intent-filter>

Looks like Viber handles Intent.ACTION_SEND with scheme sms: and smsto:.

Try to change your code like this:

        val intent = Intent(Intent.ACTION_SEND).apply {
            `package` = "com.viber.voip"
            data = Uri.parse("sms:$phoneNumber")
        }
        startActivity(intent)

Upvotes: 0

Related Questions