Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to send message programmatically by using WhatsApp, WeChat?

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

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

like image 526
Subhalaxmi Avatar asked Jul 16 '14 07:07

Subhalaxmi


People also ask

Can we send WhatsApp message programmatically?

Android intent system Simply create an intent to share text, for example, and WhatsApp will be displayed by the system picker: Intent sendIntent = new Intent(); sendIntent. setAction(Intent. ACTION_SEND); sendIntent.

How can I send WhatsApp message through web application?

Open the web browser and then paste 'https://api.WhatsApp.com/send?phone=number' in the Address bar of your phone's browser. In the place of “number”, enter the phone number of the person to whom you want to send a WhatsApp message with the country code.


3 Answers

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");
like image 89
Subhalaxmi Avatar answered Oct 21 '22 12:10

Subhalaxmi


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();
    }

}
like image 32
MysticMagicϡ Avatar answered Oct 21 '22 14:10

MysticMagicϡ


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();
    }
}
like image 15
NehaK Avatar answered Oct 21 '22 13:10

NehaK