How can I send a message via WhatsApp to a specific contact from another android application?

5

I am developing an application that sent a message to a specific WhatsApp contact from my app.

I already tried this code.

Uri mUri = Uri.parse("smsto:+999999999");
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("sms_body", "The text goes here");
mIntent.putExtra("chat",true);
startActivity(mIntent);

The contact if selected, but the problem is that the text is not received by WhatsApp, only the contact is selected.

I already use this code, and if I send the text but I have to manually select the contact so that it works

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent); 
    
asked by Johny Moo 01.04.2016 в 00:59
source

2 answers

6

Due to security issues, it is NOT allowed and it is NOT possible to pre-select the user if you want to send a WhatsApp message from an Android application.

So the only thing possible is what you say, the application opens and the client must select the user who wants to send the message:

public void enviaMensajeWhatsApp(String msj) {    
        PackageManager pm=getPackageManager();
        try {
            Intent waIntent = new Intent(Intent.ACTION_SEND);
            waIntent.setType("text/plain");             
            PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
            waIntent.setPackage("com.whatsapp");
            waIntent.putExtra(Intent.EXTRA_TEXT, msj);
            startActivity(Intent.createChooser(waIntent, "Compartir con:"));
        } catch (PackageManager.NameNotFoundException e) {
            Toast.makeText(this, "WhatsApp no esta instalado!", Toast.LENGTH_SHORT)
                    .show();
        }
    }

Executing the previous method:

enviaMensajeWhatsApp("Mi mensaje es abcdef 1234567890");

open the WhatsApp application and we have to select the user to send the message:

If you want to pre-select a user to send a message, you can do it using a Intent.ACTION_VIEW , but you require that the defined number of the contact must be registered in your phone.

The number must contain the country code and the area code, starting with the "+" sign, for example my country Mexico +52 , area code for Mexico City 55 and my phone 1234567890 :

 String msj = "Mi mensaje es abcdef 1234567890";
 String numeroTel = "+52551234567890";
 Intent intent = new Intent(Intent.ACTION_VIEW);
 String uri = "whatsapp://send?phone=" + numeroTel + "&text=" + msj;
 intent.setData(Uri.parse(uri));
 startActivity(intent);

This will open the contact window to send and the message automatically, in this case also for security a human user has to activate the sending:

    
answered by 01.04.2016 / 02:35
source
2

Modify your Intent so that it stays that way, in this case I have a function where I call it from a button for speed, but you still modify it to your needs.

prueba.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onClickWhatsApp();
        }
    });


public void onClickWhatsApp() {

    PackageManager pm=getPackageManager();
    try {

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "Tu texto aquí";

        PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
        waIntent.setPackage("com.whatsapp");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(Intent.createChooser(waIntent, "Compartir con"));

    } catch (PackageManager.NameNotFoundException e) {
        Toast.makeText(this, "WhatsApp no está instalado", Toast.LENGTH_SHORT)
                .show();
    }

}

source - > here

    
answered by 01.04.2016 в 02:06