Open conversation in Whatsapp and populate the text

别来无恙 提交于 2019-12-03 11:34:11
CommonGuy

Sadly, they didn't implement this feature on android (maybe they do in the future).

I already asked a similar question, but with no results. While whatsapp reacts to some intents like the one you show in your code, they simply don't use the text you send. I suppose this is because of safety reasons, imagine the amount of whatsapp-spammer apps in the play store... I don't know how (and why) it is implemented in iOS...

The two alternatives that come close to your solution are

  1. send the text and create a chooser (no need to type, just select whatsapp)
  2. open the contact (text needs to be typed, but no need to select the contact)

Update in 2017

based on this Whatsapp FAQ

im using this url

https://api.whatsapp.com/send?phone=62xxxxxxx&text=Hello,%20from%20my%20Apps

which 62 is Indonesia's Dialing code

then i use intent like this (im using kotlin)

val uri = Uri.parse("https://api.whatsapp.com/send?phone=62xxxxxxx&text=Hello,%20from%20my%20Apps")
        val intent = Intent(Intent.ACTION_VIEW, uri)
        startActivity(intent)

replace 62xxxxx with your number this work fine to me

BahaR

I have done it!

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

Try using the following solution to send image and text.

You can setType as "text" and remove extra_stream to use it only for sending text.

            Intent sendIntent = new Intent("android.intent.action.SEND");
            File f=new File("path to the file");
            Uri uri = Uri.fromFile(f);
            sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.ContactPicker"));
            sendIntent.setType("image");
            sendIntent.putExtra(Intent.EXTRA_STREAM,uri);
            sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("919xxxxxxxxx")+"@s.whatsapp.net");
            sendIntent.putExtra(Intent.EXTRA_TEXT,"sample text you want to send along with the image");
            startActivity(sendIntent);

EXTRA INFORMATION ABOUT THE PROCESS OF FINDING THE SOLUTION :

After reverse engineering WhatsApp, I came across the following snippet of Android manifest,

Normal Share intent, uses "SEND" which does not allow you to send to a specific contact and requires a contact picker.

The specific contact is picked up by Conversation class and uses "SEND_TO" action, but it uses sms body and can not take up image and other attachments.

 <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode" android:name="com.whatsapp.Conversation" android:theme="@style/Theme.App.CondensedActionBar" android:windowSoftInputMode="stateUnchanged">
            <intent-filter>
                <action android:name="android.intent.action.SENDTO"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="sms"/>
                <data android:scheme="smsto"/>
            </intent-filter>
        </activity>

Digging further, I came across this,

 <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode" android:name="com.whatsapp.ContactPicker" android:theme="@style/Theme.App.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.PICK"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="com.whatsapp"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="audio/*"/>
                <data android:mimeType="video/*"/>
                <data android:mimeType="image/*"/>
                <data android:mimeType="text/plain"/>
                <data android:mimeType="text/x-vcard"/>
                <data android:mimeType="application/pdf"/>
                <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document"/>
                <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
                <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation"/>
                <data android:mimeType="application/msword"/>
                <data android:mimeType="application/vnd.ms-excel"/>
                <data android:mimeType="application/vnd.ms-powerpoint"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="audio/*"/>
                <data android:mimeType="video/*"/>
                <data android:mimeType="image/*"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:host="send" android:scheme="whatsapp"/>
            </intent-filter>
            <meta-data android:name="android.service.chooser.chooser_target_service" android:value=".ContactChooserTargetService"/>
        </activity>

Finally using a decompiler for ContactPicker and Conversation class,the extra key-value for phone number was found to be "jid".

Use Below code to Open conversations in what's App

  Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setPackage("com.whatsapp");
            intent.setData(Uri.parse(String.format("https://api.whatsapp.com/send?phone=%s", "91xxxxxxxxxx")));
            if (getPackageManager().resolveActivity(intent, 0) != null) {
                startActivity(intent);
            } else {
                Toast.makeText(this, "Please install whatsApp", Toast.LENGTH_SHORT).show();
            }

Note : always enter the country code without "+" to the number.

You can use the following snippet for WhatsApp:

public void onClickWhatsApp(View view) {

    PackageManager pm=getPackageManager();
    try {

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "YOUR TEXT HERE";

        PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
        //Check if package exists or not. If not then code 
        //in catch block will be called
        waIntent.setPackage("com.whatsapp");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(Intent.createChooser(waIntent, "Share with"));

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

this code is working for me . where the jid is the phone number without '+' and this supports MODIFIED Versions of WhatsApp like WhatsApp+ etc.. only by passing the WhatsApp Package Name . you can find full Code in MyApp

   private void openChat(String text, String packageName) {
        if (text.trim().isEmpty())
            Toast.makeText(context, "Please Enter Number", Toast.LENGTH_SHORT).show();
        else {
            try {
                String smsNumber = Util.getNumber(text) + "@s.whatsapp.net";
                Uri uri = Uri.parse("smsto:" + smsNumber);
                Intent i = new Intent(Intent.ACTION_SENDTO, uri);
                i.putExtra("jid", smsNumber);
                i.setPackage(packageName);
                context.startActivity(i);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(context, "Make Sure you have WhatsApp App Installed on Your Device", Toast.LENGTH_SHORT).show();
            }
        }
    }

Method 1 - Using android component name

 public static void openWhatsAppConversation(Context context, String number, String message) {

    number = number.replace(" ", "").replace("+", "");

    Intent sendIntent = new Intent("android.intent.action.MAIN");

    sendIntent.setType("text/plain");
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
    sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(number) + "@s.whatsapp.net");

    context.startActivity(sendIntent);
}

Method 2 - Using whatsapp api uri

public static void openWhatsAppConversationUsingUri(Context context, String numberWithCountryCode, String message) {

    Uri uri = Uri.parse("https://api.whatsapp.com/send?phone=" + numberWithCountryCode + "&text=" + message);

    Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);

    context.startActivity(sendIntent);
}
public void sendMessageOnWhatsApp(String number, String text, Context context){
    String url = "https://api.whatsapp.com/send?phone="+number+"&text="+text;
    Intent whatsappIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
    try {
        context.startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        Toaster.showMessageShort(context.getString(R.string.no_whatsapp_msg));
    }

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!