How to share text to WhatsApp from my app?

AndroidAndroid IntentShareWhatsapp

Android Problem Overview


I develop an app with a functionality for sharing text. This is working fine except for WhatsApp. What should I do? Is there any specific API for that?

Android Solutions


Solution 1 - Android

You can use intent to do so. No need to use Whatsapp API. Hope that I have not misunderstood your question. Hope that helps, thanks.

Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
try {
	activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
	ToastHelper.MakeShortText("Whatsapp have not been installed.");
}

Solution 2 - Android

There are two ways to integrate with WhatsApp:

  • Through a custom URL scheme

  • Through Android's intent system.

If you have a website and want to open a WhatsApp chat with a pre-filled message, you can use our custom URL scheme to do so. Opening whatsapp://send?text= followed by the text to send, will open WhatsApp, allow the user to choose a contact, and pre-fill the input field with the specified text.

Like most social apps on Android, WhatsApp listens to intents to share media and text. 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.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

However, if you prefer to share directly to WhatsApp and bypass the system picker, you can do so by using setPackage in your intent:

sendIntent.setPackage("com.whatsapp");

This would simply be set right before you call startActivity(sendIntent);

Please refer below link Official WhatsApp Page: https://www.whatsapp.com/faq/en/android/28000012,

If you want to share some text to specific WhatsApp contact, Please refer below code.

private void openWhatsApp() {
String smsNumber = "7****"; //without '+'
try {
    Intent sendIntent = new Intent("android.intent.action.MAIN");
    //sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setType("text/plain");
    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    sendIntent.putExtra("jid", smsNumber + "@s.whatsapp.net"); //phone number without "+" prefix
    sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent);
} catch(Exception e) {
    Toast.makeText(this, "Error/n" + e.toString(), Toast.LENGTH_SHORT).show();
 }

}

For more details please refer below link https://stackoverflow.com/questions/19081654/send-text-to-specific-contact-whatsapp

Solution 3 - Android

If the user does not have the Whatsapp app in their device then the user will get the ActivityNotFoundException

Then, you should move the user to download the app first.

public void shareViaWhatsApp() {
        Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
        whatsappIntent.setType("text/plain");
        whatsappIntent.setPackage("com.whatsapp");
        whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Application of social rating share with your friend");
        try {
            Objects.requireNonNull(getActivity()).startActivity(whatsappIntent);
        } catch (android.content.ActivityNotFoundException ex) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.whatsapp")));
        }
    }

Solution 4 - Android

Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("text/plain");
    share.putExtra(Intent.EXTRA_TEXT, "Your text");
    startActivity(Intent.createChooser(share, "Share using"));

Solution 5 - Android

  Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent);

Solution 6 - Android

I am not 100% sure...but i am afraid there is no official API released. I also wanted to implement a "send us a whatsapp" feature, but i am giving up for a while until whatsapp.inc release a official one

There are some no-official API but i do not know if you want that...

http://www.whatsapp-api.com/developers.php

https://github.com/venomous0x/WhatsAPI

good luck....and if you discover something, please let me know ;)

Solution 7 - Android

Solution 8 - Android

 message = "this msg is sent from My App Time Track"
            val intent = Intent()//Empty as we don't know the destination i.e implicit intent
            intent.action = Intent.ACTION_SEND//intent will do work of sending something
            intent.putExtra(Intent.EXTRA_TEXT, message)//send given message
            intent.putExtra(Intent.EXTRA_SUBJECT,"Download Time Track App")//give the subject for your message
            //Intent.Extra_Text is actually a globol key
            intent.type = "plane/text"//type of intent

            startActivity(Intent.createChooser(intent,"Send to: "))//createChooser is a dialogBox which shows app available to send data

Solution 9 - Android

There is no public official api for whats app....So it is not possible now. (answered on 6-Nov-2012)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1755441View Question on Stackoverflow
Solution 1 - AndroidSonny NgView Answer on Stackoverflow
Solution 2 - AndroidJitendra Kumar. BallaView Answer on Stackoverflow
Solution 3 - AndroidPrince DholakiyaView Answer on Stackoverflow
Solution 4 - AndroidRutul MehtaView Answer on Stackoverflow
Solution 5 - AndroidAmrish KakadiyaView Answer on Stackoverflow
Solution 6 - AndroidRakoView Answer on Stackoverflow
Solution 7 - AndroidNathalie LimaView Answer on Stackoverflow
Solution 8 - AndroidAsimView Answer on Stackoverflow
Solution 9 - AndroidSaqqibView Answer on Stackoverflow