25

How to use messaging in android application by using WhatsApp and WeChat?

Actually requirement is to send sms using WhatsApp and WeChat (Free sms).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42
  • 1
    refer http://developers.wechat.com/wechatapi – WISHY Jul 16 '14 at 07:26
  • @Shink Thank you , I all ready Got This Sdk actually I need some tutorial how to integrate it programmatically ? – Subhalaxmi Jul 16 '14 at 07:28
  • For sending messages using Whats and we chat you need to use Api of them and authentication. – Ashfaque Jul 16 '14 at 07:28
  • there is no api for whatsapp ... u have to send Intent to it – Maveňツ Jul 16 '14 at 07:45
  • @Mann Yes , I know there is no API .. I want to no any idea how to send programmatically by choosing any from whats app and we chat ? – Subhalaxmi Jul 16 '14 at 09:02
  • THere are so many answers u got .....atlest check them out..? – Maveňツ Jul 16 '14 at 10:28
  • @SubhalaxmiNayak : I am getting this Error : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.akm.whatsapp/com.akm.whatsapp.WhatsActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO typ=text/plain pkg=com.whatsapp (has extras) } ....... also whats app is in my device – Akarsh M Sep 08 '14 at 07:51

5 Answers5

33

I got the Solution.. Here I am posting the answer so that it may help other people who may have same doubt..

For Share through any application...

public void sendAppMsg(View view) {

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    String text = " message you want to share..";
    // change with required  application package  

    intent.setPackage("PACKAGE NAME OF THE APPLICATION");
    if (intent != null) {
        intent.putExtra(Intent.EXTRA_TEXT, text);//
        startActivity(Intent.createChooser(intent, text));
    } else {

        Toast.makeText(this, "App not found", Toast.LENGTH_SHORT)
                .show();
    }
}

Note : change *PACKAGE NAME OF THE APPLICATION as per your requirement like

Example : USE

//Whatsapp
    intent.setPackage("com.whatsapp");`

//Linkedin
    intent.setPackage("com.linkedin.android");

//Twitter    
    intent.setPackage("com.twitter.android");

//Facebook
    intent.setPackage("com.facebook.katana");

//GooglePlus
    intent.setPackage("com.google.android.apps.plus");
Shreshth Kharbanda
  • 1,777
  • 14
  • 24
Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42
15

This should help to send message using whatsapp:

public void sendWhatsAppMsg() {

    Intent waIntent = new Intent(Intent.ACTION_SEND);
    waIntent.setType("text/plain");
            String text = "testing message";
    waIntent.setPackage("com.whatsapp");
    if (waIntent != null) {
        waIntent.putExtra(Intent.EXTRA_TEXT, text);//
        startActivity(Intent.createChooser(waIntent, text));
    } else {
        Toast.makeText(this, "WhatsApp not found", Toast.LENGTH_SHORT)
                .show();
    }

}
GreenROBO
  • 4,725
  • 4
  • 23
  • 43
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
15

To send direct message to any whatsapp user use following code :

private void sendMessageToWhatsAppContact(String number) {
    PackageManager packageManager = context.getPackageManager();
    Intent i = new Intent(Intent.ACTION_VIEW);
    try {
        String url = "https://api.whatsapp.com/send?phone=" + number + "&text=" + URLEncoder.encode(CommonStrings.SHARING_APP_MSG, "UTF-8");
        i.setPackage("com.whatsapp");
        i.setData(Uri.parse(url));
        if (i.resolveActivity(packageManager) != null) {
            context.startActivity(i);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
NehaK
  • 2,639
  • 1
  • 15
  • 31
  • 1
    I've tried this code. It works but it's not exactly 'sending' it. It entered the text and now the user need to press 'send' button manually. Is it possible to just directly send it without waiting for the user to press the 'send' button? – Dante Jan 05 '18 at 15:27
  • I needed same functionality you are asking.. but didn’t find any way to send it directly.. – NehaK Jan 07 '18 at 07:07
  • 2
    I posted the same comment for a similar answer in a different post and they replied that it is due to security concerns of whatsapp that they don't allow it. I find it reasonable. Imagine some unknown app using your service to send messages without the interference of user. But I'm sure it might still be possible if we can simulate a touchevent on screen. However, it IS possible to directly send the message using the usual message app without waiting for user's approval. – Dante Jan 07 '18 at 07:33
  • Google has effectively revoked SEND_SMS permission from all but default SMS apps. Am I wrong that this approach (sending through WhatsApp, say) would be a work-around? – JAW Nov 20 '18 at 19:21
  • @NehaK Hi did you get the answer for this ? using this code opening app but not sending directly – Venkatesh Dec 16 '19 at 12:21
2

you can try one of these two solutions they worked for me.

reference: https://faq.whatsapp.com/en/android/26000030/?category=5245251

private void openWhatsApp(String countryCode, String mobile) {
    String customerPhoneNumber = String.format("%s%s", countryCode, mobile);
    Uri uri = Uri.parse("smsto:" + customerPhoneNumber);
    Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
    sendIntent.setPackage("com.whatsapp");

    if(sendIntent.resolveActivity(getPackageManager()) == null){
        showDialogMessage("you should install whatsapp.");
        return;
    }

    startActivity(sendIntent);
}

or

private void openWhatsApp(String countryCode, String mobile) {
    String customerPhoneNumber = String.format("%s%s", countryCode, mobile);
    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.setPackage("com.whatsapp");
// you can remove this part ("&text=" + "your message")
    String url = "https://api.whatsapp.com/send?phone=" + customerPhoneNumber + "&text=" + "your message"; 
    sendIntent.setData(Uri.parse(url));

    if(sendIntent.resolveActivity(getPackageManager()) == null){
        showDialogMessage("you should install whatsapp.");
        return;
    }

    startActivity(sendIntent);
}
MohamedHarmoush
  • 1,033
  • 11
  • 17
0

This is the simplest example that I get

/**
 * sends message to the specific application
 * @param text The message that user wants to send
 * @param view is the activity view of the application
 */
public void sendMessage(String text, View view){
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setType("text/plain");
    intent.setPackage("com.example.appname");//For whatsapp you can use com.whatsapp
    if (intent !=null){
        intent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(intent);
    }else{
        Snackbar.make(this, "Application not found.", Snackbar.LENGTH_SHORT).show();
    }
}
gouri panda
  • 312
  • 2
  • 11