Send Whatsapp message to specific contact

前端 未结 8 2116
南旧
南旧 2020-12-05 05:27

I followed this link and this is my code

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(\"content://com.android.contacts/data/\" + \"MYNUMBER@s.whatsapp         


        
相关标签:
8条回答
  • 2020-12-05 05:57

    Try using the following solution,for image along with text.

    Just change setType("text") and remove ExtraStream if you want to send text message only.

                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".

    0 讨论(0)
  • 2020-12-05 05:58

    I use this code its working perfectly for me. And also i am checking if whats-app not install go to play store.

    private void openWhatsApp() {
        String smsNumber = "8801714884544";
        boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
        if (isWhatsappInstalled) {
            try {
                Intent sendIntent = new Intent("android.intent.action.MAIN");
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");
                sendIntent.putExtra(Intent.EXTRA_TEXT, FormViews.getTexBoxFieldValue(enquiryEditText));
                sendIntent.putExtra("jid", smsNumber + "@s.whatsapp.net"); //phone number without "+" prefix
                sendIntent.setPackage("com.whatsapp");
                startActivity(sendIntent);
            } catch(Exception e) {
                Toast.makeText(getActivity(), "Error/n" + e.toString(), Toast.LENGTH_SHORT).show();
            }
        } else {
            Uri uri = Uri.parse("market://details?id=com.whatsapp");
            Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
            Toast.makeText(getActivity(), "WhatsApp not Installed",
                    Toast.LENGTH_SHORT).show();
            getActivity().startActivity(goToMarket);
        }
    }
    
    private boolean whatsappInstalledOrNot(String uri) {
        PackageManager pm = getActivity().getPackageManager();
        boolean app_installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed;
    }
    
    0 讨论(0)
  • 2020-12-05 06:03
    private void openWhatsApp(String id) {
    
    Cursor c = getSherlockActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
            new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
            new String[] { id }, null);
    c.moveToFirst();
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
    
    startActivity(i);
    c.close();
    }
    

    Where id is what's app uri like

     0987654321@s.whatsapp.net
    
    0 讨论(0)
  • 2020-12-05 06:07

    You can try this, it works for me:

    String waUrl = "https://wa.me/918982061674?text=I+can+help+you+with+your+BA+Tuition+requirement.+Please+call+me+at+09080976510+or+check+my+UrbanPro+profile%3A+http%3A%2F%2Flocalhost%3A8080%2Ftesturl";
    
    try {
      PackageManager packageManager = context.getPackageManager();
      Intent i = new Intent(Intent.ACTION_VIEW);
      String url = waUrl;
      i.setPackage("com.whatsapp");
      i.setData(Uri.parse(url));
      if (i.resolveActivity(packageManager) != null) {
        context.startActivity(i);
      }
    } catch (Exception e) {
      Log.e("WHATSAPP", e.getMessage())
    }
    
    0 讨论(0)
  • 2020-12-05 06:12

    If you want to send message to particular user in background without opening whatsapp and you have rooted device then use following code will help you,

     protected void whatsAppSendMessage(String[] paramArrayOfString, String paramString) {
      try {
       Shell shell = Shell.startRootShell();
       int j = paramArrayOfString.length;
       for (int i = 0; i < j; i++) {
        String str3;
        long l1;
        long l2;
        int k;
        String str1;
        String str2;
        Random localRandom = new Random(20L);
    
        Log.d("AUTO",
          "ps | grep -w 'com.whatsapp' | awk '{print $2}' | xargs kill");
        commandsTestOnClick("ps | grep -w 'com.whatsapp' | awk '{print $2}' | xargs kill");
    
        str3 = paramArrayOfString[i] + "@s.whatsapp.net";
        l1 = System.currentTimeMillis();
        l2 = l1 / 1000L;
        k = localRandom.nextInt();
    
        str1 = "sqlite3 /data/data/com.whatsapp/databases/msgstore.db \"INSERT INTO messages (key_remote_jid, key_from_me, key_id, status, needs_push, data, timestamp, MEDIA_URL, media_mime_type, media_wa_type, MEDIA_SIZE, media_name , latitude, longitude, thumb_image, remote_resource, received_timestamp, send_timestamp, receipt_server_timestamp, receipt_device_timestamp, raw_data, media_hash, recipient_count, media_duration, origin)VALUES ('"
          + str3
          + "', 1,'"
          + l2
          + "-"
          + k
          + "', 0,0, '"
          + paramString
          + "',"
          + l1
          + ",'','', '0', 0,'', 0.0,0.0,'','',"
          + l1
          + ", -1, -1, -1,0 ,'',0,0,0); \"";
    
        str2 = "sqlite3 /data/data/com.whatsapp/databases/msgstore.db \"insert into chat_list (key_remote_jid) select '"
          + str3
          + "' where not exists (select 1 from chat_list where key_remote_jid='"
          + str3 + "');\"";
    
        str3 = "sqlite3 /data/data/com.whatsapp/databases/msgstore.db \"update chat_list set message_table_id = (select max(messages._id) from messages) where chat_list.key_remote_jid='"
          + str3 + "';\"";
    
        Log.d("AUTO", str1);
        Log.d("AUTO", str2);
        Log.d("AUTO", str3);
    
        shell.add(
          new SimpleCommand(
            "chmod 777 /data/data/com.whatsapp/databases/msgstore.db"))
          .waitForFinish();
        shell.add(new SimpleCommand(str1)).waitForFinish();
        shell.add(new SimpleCommand(str2)).waitForFinish();
        shell.add(new SimpleCommand(str3)).waitForFinish();
       }
       shell.close();
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
    

    Just check it out this blog for more information... I have tested it is working successfully

    http://siddhpuraamit.blogspot.in/

    0 讨论(0)
  • 2020-12-05 06:13

    Try following:

    Intent i = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("content://com.android.contacts/data/" + "MYNUMBER@s.whatsapp.net"));
    i.setPackage("com.whatsapp");
    startActivity(i);
    
    0 讨论(0)
提交回复
热议问题