Sending Message from Your Application to a WhatsApp Number

Updated 13 January 2023

Save

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.

CODE :

Now, let’s have a look at how we can open a particular WhatsApp Number on the WhatsApp application.

CODE :

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.

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.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


80 comments

  • Vikram Pandey NTPC Unchahar
    Kindly provide full code with help docs
    • Anchit Makkar
      Dear Vikram,

      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.

    • Khalsa Agency
      THANKS you
  • Varun
    Hi, how do we send the message automatically?
    • anchit (Moderator)
      Actually, I have no clear idea of your requirement/use case.
      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.
      • Varun
        Hi Anchit, You have any place to get some idea how to do that?
        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?
        • anchit (Moderator)
          Hi Varun, I have not worked on Something like that.
          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.

          • rohit sharma
            everytime i want to send a message i have to go to whatsapp to send the message but i want to execute like sending sms without calling the intent function is it possible for whatsapp please let me know if it is possible
            • anchit (Moderator)
              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.

              I am not sure but i think what you want is possible.

    • dor
      you found the way how to do it??
    • Shubham
      You can use appium testing to automatically send the message by finding the id of whatsapp send button
  • Tejpal
    how to attache image while sending msg to contact?
    • anchit (Moderator)
      In order to send an image to WhatsApp you will need to first check what the source for your image is ?
      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
      • Muhammad Taha
        What string are u referring to, while trying to attach image from its URL(image isn’t on the device)?
        • anchit
          Hi Taha,
          The String I am referring to is the text to send.
  • Yunus Emre ÇİMEN
    How to send multiple numbers?
    • anchit (Moderator)
      Hi Yunus,

      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.

  • Esteban
    This example work to Xamarin Forms?
    • anchit (Moderator)
      Hi esteban,

      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.

  • sai prudhvi reddy Gayam
    everything worked fine but one had to put their country code before your number then only it is going to work.
    • anchit (Moderator)
      Well the phone number need to be fully qualified number without the plus sign or leading zeros.

      Suppose your country is India and your phone number is “xxxxxxxxxx”, then you need to send the “91xxxxxxxxxx” in the phone number field.

      • anchit (Moderator)
        Well, Thank You Sai Prudhvi Reddy Gayam, I have updated this in the blog as well so that it helps the furture readers.
  • s
    can i send image also by this process?
    if your ans is yes ,so plz tell me how is it?
    • anchit (Moderator)
      Well, Personally I have not tried this piece of code, so I am not 100% sure.

      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.

      • Aditya
        Can we send this to particular contact?
        • anchit (Moderator)
          As Per my knowledge no we cannot.
      • Aditya
        Can we give mobile number as we did to text?
        • Aditya
          so that only that conatct will get that
          • anchit (Moderator)
            officialy from WhatsApp there is no such method
        • anchit (Moderator)
          Well , if you can convert the pdf file to binary byte array then perhaps you can do what you want by using the intent with Action send.
  • Aditya
    Hello I want to share pdf to specific contact. Is it possible?
    • anchit (Moderator)
      Well, officially from whatsApp there is no such method but you can try doing this with the help of intent with action action send and passing your pdf file as text argument of the inten in the form of byte array.

      Do let me know if this works out for you.

      • Aditya
        Can you show some code for this
        • anchit (Moderator)
          Well, writing and experimenting this piece of code can be time taking and I am actually a lot busy on a personal level.

          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 🙂

  • Jyoti
    Can you please mention which packages add into last piece of code ? because I’m new to android development and I tried that code but it shows error on startActivity(intent); portion i.e. cannot find symbol..
    • anchit (Moderator)
      Hello Jyoti,

      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.

  • Shreshtha
    Can we send a whastapp msg to multiple numbers using this method?
    • anchit (Moderator)
      Well, it depends entirely on your use -case.

      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 🙂

  • Harish Gaddam
    Hi, sir
    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

    • anchit (Moderator)
      Hi harish,

      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.

  • Chua Yew Beng
    Hi,

    Can this code apply in Xamarin form? What we need to add for Nuget Packages?
    Thank you!

    • anchit (Moderator)
      Hello,

      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 🙂

  • J
    Thanks a lot
    Resolve my problem.
    You are the Best!!!
    • anchit (Moderator)
      You are welcome 🙂
  • Andre
    Hello Anchit, is It currently working ? Could we talk about making some new functions?
    • anchit (Moderator)
      yes, this is working and we can definitely talk about making new functions as explained to you over the email as well.
  • Deepak
    How Can Send Attachment like PDF/Excel/Word and Image also ?
    • anchit (Moderator)
      Well, It depends on where exactly your file is?

      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.

  • Oswaldo
    Hello, how do we send the message automatically without having to press the send button?, Or any idea how to do it, thanks
    • anchit (Moderator)
      Hello,
      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.

  • Kailas Bhakade
    Is there any option to check whether given number is register on whats app or not?
    • anchit (Moderator)
      Hello,
      As of now there is no Api that informs you if a given number is registered on whatsapp or not.
      • Kailas Bhakade
        Okay, Thanks
        • anchit (Moderator)
          Your Welcome 🙂
  • YOGAPAARTIBAN AP
    Hi Anchit Makkar, I design a website for company were they require the following,
    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.

    • anchit (Moderator)
      Hello Dear,

      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 s[email protected] and our sales team will get in touch with you.

  • raju
    hello how to same way with image file in andorid
    • anchit (Moderator)
      Well, It depends on where exactly your file is?
      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.

  • Esmat M. Khairy
    Is there some name spaces should declared for this code?
    • anchit (Moderator)
      Hello there,

      No, you don’t need any additional namespace declarations for this code.

      • sachin
        how do i get the Intent Api
        • anchit (Moderator)
          intent is already defined in the android code.
  • Karthik
    if i use this code in java i fully get error
    • anchit (Moderator)
      Are you coding for android ?
  • Vaishnavi
    I am not able to import intent in eclipse. I create a MAVEN project and I am not able to import it.Can you help me please.
    • anchit (Moderator)
      Intent is something pre defined in android environment.
  • Akshay kolhe
    thanks, its working for me.
    • anchit (Moderator)
      Your Welcome
  • Mitali
    Hi Anchit,
    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?
    • anchit (Moderator)
      Hello Mitali,

      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.

  • rakesh
    thanks
    • anchit (Moderator)
      Your Welcome
  • waheed
    hi sir please help me how can image with text send wataspp number from
    our andorid app
    • anchit (Moderator)

      Hi Waheed,

      Please check the earlier comments.

      This has already been discussed multiple times in the previous comments.

  • Jitendar
    Dear Anchit,
    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);