8

Here i am stuck from few days, i am trying to send sms(that predefined set from my app ) with selected contact to whats app, with the help of stackoverflow i found two method to send sms to whatsapp.

1st method: one method is forward sms to whatsapp well, but not with selected contact, contact can be select from whatsapp but i not want that. I want contact also should be select from my app.now

2nd method: second method is opposite to first. This method send selected contact to whatsapp for sms but not with (predefined sms). My problem is that i want both things should be passed to whatsapp from my app sms+Contact.When i try merge both method gives error. Please Help me here is my code for 1st method and 2nd method.

    //1st method: for forward sms to whatsapp
        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "YOUR TEXT HERE";
        waIntent.setPackage("com.whatsapp");
        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(waIntent);


    //2nd method: send sms with selected contact but not with predefined sms 
       void openWhatsappContact(String number) {
       Uri uri = Uri.parse("smsto:" + number);
       Intent i = new Intent(Intent.ACTION_SENDTO, uri);
       i.setPackage("com.whatsapp");
       startActivity(i);


  //here i am trying to send both SMS and contact from my Android app but not working and get error. 
        Uri uri = Uri.parse("smsto:" + "065214585756");
        Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "Hai Good Morning");
        sendIntent.putExtra("chat",true);
        sendIntent.setType("text/plain");
        sendIntent.setPackage("com.whatsapp");
        startActivity(sendIntent);

Please any one help me.How i can do it.

LWC
  • 1,084
  • 1
  • 10
  • 28
sajid Hussain
  • 385
  • 1
  • 6
  • 15

4 Answers4

0

Untill and unless Whatsapp developers expose API s to send messages programmatically it cant be done. As far as i know only Google Now has a special permission for sending messages in Whatsapp, you can check the same in Google Now (Voice message send) .

Let me know if any issues.

Best Regds

RaghavPai
  • 652
  • 9
  • 14
  • question is not about sending messages its about **sending messages with a particular contact** read the question first ! – Charuක Feb 08 '17 at 19:13
0

As far as I know, you can´t do it. There's no publicly exposed API for whatsapp.

ldepablo
  • 424
  • 6
  • 19
0

You can't. WhatsApp allows the user to pick only contact or group manually , and you cannot change that in any way. Your both methods are fine.

for more details

https://www.whatsapp.com/faq/android/28000012

Ajay Venugopal
  • 1,544
  • 1
  • 17
  • 30
0

If you want to send a message to the particular number then you can use this code. It works for me.

// number should be with country code but without plus(+) and any space. For example 919876543210
private void send(String number, String message) {
    Intent sendIntent = new Intent("android.intent.action.MAIN");
    sendIntent.putExtra("jid", number + "@s.whatsapp.net");
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setPackage("com.whatsapp");
    sendIntent.setType("text/plain");
    startActivity(sendIntent);
}

It will directly open whatsapp with the particular then you just need to press Enter or Tap send button.

Enjoy it.