Sending message through WhatsApp
UPDATE
Please refer to https://faq.whatsapp.com/en/android/26000030/?category=5245251
WhatsApp's Click to Chat feature allows you to begin a chat with
someone without having their phone number saved in your phone's
address book. As long as you know this person’s phone number, you can
create a link that will allow you to start a chat with them.Use: https://wa.me/15551234567
Don't use: https://wa.me/+001-(555)1234567
Example: https://wa.me/15551234567?text=I'm%20interested%20in%20your%20car%20for%20sale
Original answer
Here is the solution
public void onClickWhatsApp(View view) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
Also see http://www.whatsapp.com/faq/en/android/28000012
Send text to specific contact programmatically (whatsapp)
This is now possible through the WhatsApp Business API. Only businesses may apply to use it. This is the only way to directly send messages to phone numbers, without any human interaction.
Sending normal messages is free. It looks like you need to host a MySQL database and the WhatsApp Business Client on your server.
How to send whatsapp message in whatsapp-web.js
Here is how I send messages in whatsapp-web.js
.
const number = "9830121234";
const sanitized_number = number.toString().replace(/[- )(]/g, ""); // remove unnecessary chars from the number
const final_number = `91${sanitized_number.substring(sanitized_number.length - 10)}`; // add 91 before the number here 91 is country code of India
const number_details = await client.getNumberId(final_number); // get mobile number details
if (number_details) {
const sendMessageData = await client.sendMessage(number_details._serialized, sendData.message); // send message
} else {
console.log(final_number, "Mobile number is not registered");
}
.
.
update (for await is valid inside only async error):
if you are using this method in any event you should put it inside an async function
on.any_kind_of_event(async function () {
const number = "9830121234";
const sanitized_number = number.toString().replace(/[- )(]/g, ""); // remove unnecessary chars from the number
const final_number = `91${sanitized_number.substring(sanitized_number.length - 10)}`; // add 91 before the number here 91 is country code of India
const number_details = await client.getNumberId(final_number); // get mobile number details
if (number_details) {
const sendMessageData = await client.sendMessage(number_details._serialized, sendData.message); // send message
} else {
console.log(final_number, "Mobile number is not registered");
}
});
Mobile website WhatsApp button to send message to a specific number
Format to send a WhatsApp message to a specific number (updated Nov 2018)
<a href="https://wa.me/whatsappphonenumber/?text=urlencodedtext"></a>
where
whatsappphonenumber is a full phone number in international format
urlencodedtext is the URL-encoded pre-filled message.
Example:
Create a link with a pre-filled message that will
automatically appear in the text field of a chat, to be sent to a specific numberSend I am interested in your car for sale to +001-(555)1234567
https://wa.me/15551234567?text=I%20am%20interested%20in%20your%20car%20for%20sale
Note :
Use: https://wa.me/15551234567
Don't use: https://wa.me/+001-(555)1234567
Create a link with just a pre-filled message that will
automatically appear in the text field of a chat, number will be chosen by the userSend I am enquiring about the apartment listing
https://wa.me/?text=I%20am%20enquiring%20about%20the%20apartment%20listing
After clicking on the link, user will be shown a list of contacts they
can send the pre-filled message to.
For more information, see https://www.whatsapp.com/faq/en/general/26000030
--
P.S : Older format (before updation) for reference
<a href="https://api.whatsapp.com/send?phone=whatsappphonenumber&text=urlencodedtext"></a>
Related Topics
How to Align Views at the Bottom of the Screen
Error Type 3 Error: Activity Class {} Does Not Exist
How to Check If Location Services Are Enabled
Text Size and Different Android Screen Sizes
Android: Storing Username and Password
Android: How to Validate Edittext Input
How to Make Ellipsize="Marquee" Always Scroll
Android Material and Appcompat Manifest Merger Failed
How to Prevent Screen Capture in Android
Where Is Debug.Keystore in Android Studio
Android - How to Disable the Click of Home Button
Integrate Mupdf Reader in an App