Search code examples
androidkotlintexthyperlinkwhatsapp

Is it possible to send a hyperlink through WhatsApp?


I am developing an application that sends a message and a link through WhatsApp. To make the appearance more aesthetic, the people behind the application want to send a link within the text in the form of a hyperlink. Is this possible??

Currently, I am sending an invitation to the client to join the WhatsApp group:

private fun shareWhatsAppGroup(mobileNumber: String) {
        val intent = Intent(Intent.ACTION_VIEW)

        val groupUri = sharedPreferences.getString(
            "my_string",
            "https://chat.whatsapp.com/group_link"
        )
        var messageOne = "Rooster Gallery welcomes you 🤍"
        var messageTwo = "Please join our WhatsApp group"
        var messageThree = "By clicking on the following link:"
        var message = messageOne + "\n" + messageTwo + "\n" + messageThree + "\n" + "$groupUri"
        intent.data =
            Uri.parse("https://api.whatsapp.com/send?phone=+962$mobileNumber&text=$message ")
        //intent.setPackage("com.whatsapp")
        startActivity(intent)
        if (!isAccessibilityOn(this, WhatsappAccessibilityService::class.java)) {
            Toast.makeText(this, "Contact don't have whatsapp", Toast.LENGTH_SHORT).show()

            val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
            this.startActivity(intent)
        }
    }

Solution

  • Just encode the message for proper URL formatting. Then whatsapp will detect that this is a link.

    var message = "$messageOne\n$messageTwo\n$messageThree\n$groupUri"
    val encodedMessage = URLEncoder.encode(message, "UTF-8")
    intent.data = Uri.parse("https://api.whatsapp.com/send?phone=+962$mobileNumber&text=$encodedMessage")