Read complete story

Sending message from your application to a WhatsApp Number

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 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 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 the WhatsApp is installed or not and if WhatsApp is not installed, this will redirect to Google play store for installing the WhatsApp.

That’s All.

Keep coding and Keep Sharing.

 

. . .

Comments (38)

Add Your Comment

  • 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
      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
          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
              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
      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
      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
      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
      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
        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
      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
          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
            officialy from WhatsApp there is no such method
        • anchit
          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
      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
          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
      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
      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 🙂

  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close