Sending message through WhatsApp

前端 未结 23 2635
萌比男神i
萌比男神i 2020-11-22 09:42

Since I found some older posts, that tell that whatsapp doesn\'t support this, I was wondering if something had changed and if there is a way to open a whatsapp \'chat\' wit

23条回答
  •  情书的邮戳
    2020-11-22 10:12

    private fun sendWhatsappMessage(phoneNumber:String, text:String) {        
      val url = if (Intent().setPackage("com.whatsapp").resolveActivity(packageManager) != null) {
            "whatsapp://send?text=Hello&phone=$phoneNumber"
        } else {
            "https://api.whatsapp.com/send?phone=$phoneNumber&text=$text"
        }
    
        val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
        startActivity(browserIntent)
    }
    

    This is a much easier way to achieve this. This code checks if whatsapp is installed on the device. If it is installed, it bypasses the system picker and goes to the contact on whatsapp and prefields the text in the chat. If not installed it opens whatsapp link on your web browser.

提交回复
热议问题