2

In my android application after user fill textbox of mobile no and message which he wants to send to that mobile number and click send button, that message should be sent to that mobile number directly without making the user to click send button in WhatsApp app. enter image description here

Below is the code I am using for sending WhatsApp message.

Uri mUri = Uri.parse("https://api.whatsapp.com/send?phone=9197xxxxxx88&text='Hello User'");
Intent intent = new Intent("android.intent.action.VIEW", mUri);
intent.setPackage("com.whatsapp");
startActivity(intent);
ashish pandey
  • 1,653
  • 1
  • 10
  • 15

4 Answers4

5

I believe you are looking for an What app API that will do the sending part for you.. AFAIK there is no such API exposed by Whats app for similar purpose.

Shoaeb
  • 709
  • 7
  • 18
2

WhatApp is not providing any API that will do the sending part for you.

You can try with below code:

                var toNumber = "+91 xxxxx xxxxx" // contains spaces.
                toNumber = toNumber.replace("+", "").replace(" ", "")

                val sendIntent = Intent("android.intent.action.MAIN")
                sendIntent.putExtra("jid", "$toNumber@s.whatsapp.net")
                sendIntent.putExtra(Intent.EXTRA_TEXT, "Hello")
                sendIntent.action = Intent.ACTION_SEND
                sendIntent.setPackage("com.whatsapp")
                sendIntent.type = "text/plain"
                startActivity(sendIntent)

I hope It will help you..

Chetan Pushpad
  • 345
  • 1
  • 9
  • the problem with your code is, it makes a user press send button in Whatsapp app. – ashish pandey Jan 16 '19 at 05:30
  • So directly You can not send the message in whats app. Whatsapp not providing such facility. If you want to send the message, you have to use chooser and you have to press on send button. – Chetan Pushpad Jan 21 '19 at 09:03
2

You can do that by using accessibility in android

Here you can find example like what you want Send message via whatsapp programmatically

Mahmoud Abu Alheja
  • 3,343
  • 2
  • 24
  • 41
1

Below code can be used to send a predefined text to a specific number. It will open IB of that number. However, it can't automatically send for you.

 startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse(
"https://api.whatsapp.com/send?phone=+91xxxxxxxxxx&text=Hey%20there% )));
DEXTOR_ANTONY
  • 300
  • 1
  • 12