send whatsapp messages to someone not on contact list from android app

2.9k views Asked by At

I'm developing an application that can send message through whatsapp. I have implemented it by following this answer but it only works for contacts that on my contact list. It keeps error when i try to send message to contacts that are not in my contact list although the user already registered on whatsapp using that number (contact). Here is the message from whatsapp:

message from whatsapp

here is my code:

    try {
        Uri uri = Uri.parse("smsto: " + smsNumber);
        //Timber.e("smsNumber %s", uri.toString());
        Intent i = new Intent(Intent.ACTION_SENDTO, uri);
        i.setPackage("com.whatsapp");
        startActivity(Intent.createChooser(i, ""));

    } catch (Exception e) {
        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
    }

is there any solutions ?

1

There are 1 answers

0
itay83 On

you can send via WhatsApp API even if he is not in your contact

String whatsappUrl = String.format("https://api.whatsapp.com/send?phone=%s&text%s",PhoneNumber,msg);
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(whatsappUrl));
            browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            p_context.startActivity(browserIntent);