Updated 13 January 2023
In this blog. we will learn about sending a message from your application to a WhatsApp Number and how you can open a particular contact in WhatsApp.
While building applications many times we need to provide the user with a facility of contacting the service providers and WhatsApp messaging in today’s World is much more popular.
Opening a particular contact or sending some particular text/url/ images to WhatsApp is a task but combining them both and that too with not so much clear documentation is a hurdle that needs to be overcome. And After Following this blog, you will see that reading the documentation is hard than implementing the same.
First, let’s have a look at the code snippet to send a particular message from your application to WhatsApp Application. As per the official documents and my implementation of the docs you can directly use the below-shared Code Segment for the aforesaid purpose.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public void openWhatsApp(View view){ PackageManager pm=getPackageManager(); try { Intent waIntent = new Intent(Intent.ACTION_SEND); waIntent.setType("text/plain"); String text = "This is a Test"; // Replace with your own message. 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 (PackageManager.NameNotFoundException e) { Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT) .show(); }catch(Exception e){ e.printStacktrace(); } } |
Now, let’s have a look at how we can open a particular WhatsApp Number on the WhatsApp application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public void openWhatsApp(View view){ PackageManager pm=getPackageManager(); try { String toNumber = "xxxxxxxxxx"; // Replace with mobile phone number without +Sign or leading zeros, but with country code. //Suppose your country is India and your phone number is “xxxxxxxxxx”, then you need to send “91xxxxxxxxxx”. Intent sendIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:" + "" + toNumber /*+ "?body=" + "")); sendIntent.setPackage("com.whatsapp"); startActivity(sendIntent); } catch (Exception e){ e.printStackTrace(); Toast.makeText(MainActivity.this,"it may be you dont have whats app",Toast.LENGTH_LONG).show(); } } |
But if you want to send a particular message to a particular user from your application to WhatsApp. All you need to do is to implement this segment of code and everything will work fine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void openWhatsApp(View view){ try { String text = "This is a test";// Replace with your message. String toNumber = "xxxxxxxxxx"; // Replace with mobile phone number without +Sign or leading zeros, but with country code //Suppose your country is India and your phone number is “xxxxxxxxxx”, then you need to send “91xxxxxxxxxx”. Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://api.whatsapp.com/send?phone="+toNumber +"&text="+text)); startActivity(intent); } catch (Exception e){ e.printStackTrace(); } } |
I prefer using the last piece of code as it will automatically check if WhatsApp is installed or not and if WhatsApp is not installed, this will redirect to the Google play store for installing WhatsApp.
That’s All. Keep coding and Keep Sharing.
We have also developed many WhatsApp integrations for e-commerce platforms including Magento 2 WhatsApp Extension, which allows customers to contact the store admin from the social app.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
80 comments
The code snippet in the blog are complete. All you need to do is adjust the message and contact number as per your use.
The explanation in the blog as per me is sufficient for any reader to read and implement this functionality.
Still, if you have doubts in any point do let me know and i will try to improvise the same in blog.
Moreover, if you still have any errors in the code then share the code with me and i will help you out, regarding the same.
But as I understood, you need to automate the message being sent from your application to WhatsApp, you can try using the BroadcastRecievers(Job Schedulers) and Services to achieve a working solution as per your need.
Ideally, I want to create a way to pick up messages and recipient numbers and send the message automatically, without clicking on the Send button?
But as far as I know
1) you will need to create a custom service that will request the access. This access will actually notify you when the message is filled in the edit text of the whatsapp ui.
2) You will then have to automate and perform the click on the send button of the WhatsApp Ui. (which some say is impossible due to WhatsApp restriction, I am not 100% sure).
For starting you can try reading AccessibilityService from the core android packages and then modifying it to achieve what you want.
Please note that in this case the approach with the use of “http://api.whatsapp.com/send?phone=” might not work and you might need to redefine your whole logic based on the default Intents and Actions and open WhatsApp using that.
Please do let me know if you actually get a working solution for that.
But as someone in other comments suggested using appium or something that can really find the send button and automatically push the message.
I am not sure but i think what you want is possible.
If this is just another url then you can directly, append the url in the string.
If you want to attach the image as file then you can refer this article : https://stackoverflow.com/a/42435212/7674961
The String I am referring to is the text to send.
Well I Am Afraid there is no direct api/ method to send a single message to multiple numbers officially.
Also, for the approach shared in the article it requires user to press the final send button on the whatsApp Screen so that the mesaage actually is delivered to the intended number.
If still you would want to send it to multiple devices, then I would advise you
1) To create a array/ array list in which all the numbers are stored
2) Create a Reciever that is trigerred when your user comes back to your application
3) Simply repeat the process used for sending to a single number until your end of array is reached.
Well, I have no exposure to Xamarin Forms.
But the approaches discussed in the blog will work definitively for core android as it is using only intents and uri scheme.
Suppose your country is India and your phone number is “xxxxxxxxxx”, then you need to send the “91xxxxxxxxxx” in the phone number field.
if your ans is yes ,so plz tell me how is it?
But if i was in your place, i would do something like this–>
public void shareImageOnWhatsApp(Uri imgBitmapUri){
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
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.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
waIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
waIntent.setType(“image/png”);
waIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
waIntent.putExtra(Intent.EXTRA_TEXT, “My Custom Text “);
waIntent.putExtra(Intent.EXTRA_SUBJECT, “Subject text”);
startActivity(Intent.createChooser(waIntent, “Share this”));
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(this, “WhatsApp not Installed”, Toast.LENGTH_SHORT)
.show();
}catch(Exception e){
e.printStacktrace();
}
}
Please note that you need to pass the Uri of the image file you want to share as an argument to this function.
Do let me know if this works out for you.
So, I am sorry but I will not be able to provide you with exact code.
But if I was in your place, I would use a fusion of the second approach in the blog and image sharing code i have described in previous comments.
Hope this helps You 🙂
Can you please do let me know where exactly have you written this line.
I mean to say inside some activity class or fragment or any other class.
Please do elaborate on this part so that we can help you in a better way.
If you have a list of contacts then obviously you can use a for loop for iterating over the numbers that you want to send the message to.
The api by default supports a single message to a single number only.
You can also try sending multiple numbers as comma separated strings in the last approach shared in the blog.
Do let me know if this works out for you 🙂
I need to send an invitation from my created app to facebook social media contacts as like whatsapp inorder to install my created app
how to do for facebook api and email api , invite from my created app.
Thanks & Regards,
Harish Gaddam
ok, so you need to send the invitation link over email & facebook?
as per my understanding for email you can just use the correct intent with proper flags and you will be able to do that.
For Facebook you need to ahave a look into the facebook sdk documentation, enable the same from their developer console and then use that feature as per thier instructions.
Can this code apply in Xamarin form? What we need to add for Nuget Packages?
Thank you!
Well, I have no personal experience for the Xamarin form and how that works.
But the last approach mentioned in this blog should definitely work for Xamarin form as well.
The last approach actually uses an http request and the message and the reciever’s phone number are appended in the url itself.
So, this should work fine.
Http Request Url –> “http://api.whatsapp.com/send?phone=”+toNumber +”&text=”+text
Hope this helps you 🙂
Resolve my problem.
You are the Best!!!
if your file is on any public url , then you can try sending the url as text params as mentioned in the first and third approach of the blog.
But if you are going to send a raw file then exactly there is no proper documentation for you. But you can try doing this with the help of intent with action send and passing your pdf file as text argument of the intent in the form of byte array.
Well, actually there is no official way to send a message without having to press the send button.
I personally have no experience for this part.
But as someone in other comments suggested using appium or something that can really find the send button and automatically push the message.
As of now there is no Api that informs you if a given number is registered on whatsapp or not.
1. Need the website to send thousands of whatsapp messages.
2. Need to tell me how many messages can be sent every day or every hour.
3. Need to be able to choose between: text, image, audio or video messages.
4. The list of contacts should be read from a plain text file.
5. Must be able to use several senders (channels) also loaded from a text file.
6. Reports need to show how many messages are successfully sent.
Please help me how to modify your last code to achieve these parameters.
Thank you.
Do you need any logical explanations for this implementation of the requirement above?
–> The points you have mentioned above can be correctly answered by the WhatsApp Support Team. I would suggest you contact them.
Or
Do you want us to Write Code for you and Deploy?
–> For this, please do mail your complete requirement to [email protected] and our sales team will get in touch with you.
if your file is on any public url , then you can try sending the url as text params as mentioned in the first and third approach of the blog.
But if you are going to send a raw file then exactly there is no proper documentation for you. But you can try doing this with the help of intent with action send and passing your pdf file as text argument of the intent in the form of byte array.
No, you don’t need any additional namespace declarations for this code.
Great efforts.
this code is for mobile application right?
i want to do implement same feature in my java application.
any idea how to do it?
The approaches and code depicted is Android only.
For the java applications, you can use the url parsings described in this article only.
But you will also need to find an alternative to the intent class of the android which actually is passing the information to the whatsapp in the first approach,
in the second and third approach, the intent is just used to open the url in the default browser of the android phone of the user and the res of the part will be managed by the Whatsapp application itself.
our andorid app
Hi Waheed,
Please check the earlier comments.
This has already been discussed multiple times in the previous comments.
Thanks for your effort.
Following code works good.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(“http://api.whatsapp.com/send?phone=”+toNumber +”&text=”+text));
startActivity(intent);
It opens the WhatsApp app and waits for us to press the Send or Right Arrow button.
Our requirement is it must automatically send the WhatsApp message like the SMS.
String phoneNumber = String.valueOf(phone.getText());
String myLatitude = String.valueOf(location.getLatitude());
String myLongitude = String.valueOf(location.getLongitude());
String message = “https://maps.google.com/?q=”+myLatitude+”,”+myLongitude;
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber,null,message,null,null);
I am not sure if the approaches discussed above will work or not.
But , yeah , you can give them a try.
If you want to change this flow then you will have to create your own ui and ask the number first from the user before hitting the url.
The url once fired will work as per the default flow from whatsapp and android deeplinking combined.