0

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!

Tony Yang
  • 53
  • 1
  • 10

1 Answers1

1

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.

Jay Mungara
  • 6,663
  • 2
  • 27
  • 49
  • Can I send message by contact name instead of phone number ? – Tony Yang Aug 14 '18 at 06:48
  • No, I don't think so. But, Still you want to do it then what you can do is retrieve the list of contacts from Content provider and then compare your username with list of contacts names and then you can send the text message to the number of the name which matched your username. I hope this answer is helpfull for you. – Jay Mungara Aug 14 '18 at 07:37
  • Make sense! Thanks for your help. – Tony Yang Aug 14 '18 at 08:27
  • This can help me to get to the specific message area by number with text, do you know how to send directly without pressing the send by users? I could not find this, all I found was everyone says we can not control other application to send the message because of the security concern, but I think there should be a way to emulate the press action or so. Plus, I have used an application that can send directly, its name "kika go." – Tony Yang Aug 16 '18 at 03:11
  • I don't know. But, If i will find something i will let you know about it. – Jay Mungara Aug 16 '18 at 04:27