1

I need to open the WhatsApp chat window to a specific number without passing a message in the intent

I tried to remove the sendIntent.putExtra(Intent.EXTRA_TEXT, userText) but didn't help, when I send from the app it says

"Can't send empty message"

Here is the complete code

    val sendIntent = Intent("android.intent.action.MAIN")
    sendIntent.action = Intent.ACTION_SEND
    sendIntent.setPackage("com.whatsapp")
    sendIntent.type = "text/plain"
    sendIntent.putExtra("jid", number + "@s.whatsapp.net")
    sendIntent.putExtra(Intent.EXTRA_TEXT, userText)
    startActivity(sendIntent)
AgentP
  • 6,261
  • 2
  • 31
  • 52
  • This link might help https://stackoverflow.com/questions/15462874/sending-message-through-whatsapp – Nooruddin Lakhani Aug 31 '19 at 04:39
  • You can check this https://stackoverflow.com/questions/38277585/want-to-open-whatsapp-to-chat-with-a-unsaved-contact-number-in-android-app and this https://medium.com/@mujtahidah/send-whatsapp-to-specific-number-android-studio-96c8caf7422f – Nooruddin Lakhani Aug 31 '19 at 04:44

1 Answers1

3

You can use WhatsApp uri to open a specific whatsapp chat without using any message like this. Here num can be any valid number including country code like 911234567890

private fun openWhatsApp(num: String) {
    val isAppInstalled = appInstalledOrNot("com.whatsapp")
    if (isAppInstalled) {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://api.whatsapp.com/send?phone=$num"))
        startActivity(intent)
    } else {
        // WhatsApp not installed show toast or dialog
    }
}

To check if WhatsApp is installed you can use this method

 private fun appInstalledOrNot(uri: String): Boolean {
    val pm = requireActivity().packageManager
    return try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES)
        true
    } catch (e: PackageManager.NameNotFoundException) {
        false
    }
}
Somesh Kumar
  • 8,088
  • 4
  • 33
  • 49