0

I'm trying to send a message to Whatsapp from another app, that I achieved with the following code, and it's working fine:

To send simple messages refer: Send text to specific contact programmatically (whatsapp)

fun sendWhatsappMessage(phone: String, message: String) {
    val packageManager = packageManager
    val i = Intent(Intent.ACTION_VIEW)

    try {
        val url = "https://api.whatsapp.com/send?phone=$phone&text=" + URLEncoder.encode(
            message,
            "UTF-8"
        )
        i.setPackage("com.whatsapp")
        i.data = Uri.parse(url)
        if (i.resolveActivity(packageManager) != null) {
            sendBroadcast(i)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

}

My question is, is there a way to send a complex message, such as contact info through the message?

Prashant Sable
  • 1,003
  • 7
  • 19
Luciano Ferruzzi
  • 1,042
  • 9
  • 20

1 Answers1

0

Try to send contact using the vcard uri from contacts:

Uri uri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, <contact key>);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(ContactsContract.Contacts.CONTENT_VCARD_TYPE);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.putExtra(Intent.EXTRA_SUBJECT, "Charlinho"); // Input contact name
Bruno Martins
  • 1,347
  • 2
  • 11
  • 32