Ori Frish
Ori Frish

Reputation: 411

Send message to user in WhatsApp from my app (Android)

I've gone through plenty of WhatsApp posts here in StackOverflow.

Like these: Is it legal to use WhatsAPI?

Android Whatsapp/Chat Examples

Sending message through WhatsApp

My question is this. I manage to send a message from my app to WhatsApp for someone that is in my contact list.

However, I want to send a message (NOT SPAM!) to someone who is not on my Contact List through WhatsApp, and I'm unable to do so with the given solutions.

How is it possible?

By the way, how is it possible to fill the body of a WhatsApp text field with a pre-defined message, so the user can edit or send immediately? "sms_body", or Intent.EXTRA_TEXT doesn't seem to work...

public void shareWhatsApp(String whatsappid) {

    try {
        Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
                new String[] { whatsappid }, null);
        c.moveToFirst();
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
        i.putExtra(Intent.EXTRA_TEXT, "Hello!");

        startActivity(i);
        c.close();
    } catch (Exception e) {
        Toast.makeText(this, "Install WhatsApp First", Toast.LENGTH_LONG).show();;
        e.printStackTrace();
    }
}

Upvotes: 6

Views: 9318

Answers (3)

Nikhil Vaish
Nikhil Vaish

Reputation: 11

You should only access Call Log or SMS permissions when your app falls within permitted uses and only to enable your app’s critical core functionality.

Core functionality is defined as the main purpose of the app. This may comprise of a set of core features, which must all be prominently documented and promoted in the app's description. Without the core feature(s), the app is "broken" or rendered unusable. link -https://support.google.com/googleplay/android-developer/answer/9047303?source=post_page-----9b8226de7827----------------------

Upvotes: 1

Crono
Crono

Reputation: 2054

I answered this question recently and it works very well, please search my answer here:

Send text to specific contact programmatically (whatsapp)

It really works to send a specific message to a specific contact in WhatsApp from your own Android app,

Enjoy! :)

Upvotes: 0

Yoraco Gonzales
Yoraco Gonzales

Reputation: 737

You will need some EXTRAS to put into the Intent. Also you may use the phone number instead of the id.

See this solution here

Upvotes: 0

Related Questions