Tony Yang
Tony Yang

Reputation: 63

Send message to specific contact in WhatsApp on android studio

I want to know how to send message by contact's name to WhatsApp in my own application.

I have found several similar questions, but only can send message by phone number, such as this

Send text to specific contact programmatically (whatsapp)

Dose anyone can help me to solve this problem? Thank you very much!

Upvotes: 0

Views: 3218

Answers (1)

Jay Mungara
Jay Mungara

Reputation: 7150

you just have to perform the following code:

try {
        Intent sendMsg = new Intent(Intent.ACTION_VIEW);
        String url = "https://api.whatsapp.com/send?phone=" + "+92 1111111111" + "&text=" + URLEncoder.encode("Your Message to Contact Number", "UTF-8");
        sendMsg.setPackage("com.whatsapp");
        sendMsg.setData(Uri.parse(url));
        if (sendMsg.resolveActivity(getPackageManager()) != null) {
            startActivity(sendMsg);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Phone number should be in correct format as I have passed like 92 for Country code and other 10 digits for your Phone number and you can also pass the message you want.

Upvotes: 1

Related Questions