How can I send message to specific contact through WhatsApp from my android app?

前端 未结 13 1562
梦毁少年i
梦毁少年i 2020-12-02 17:41

I am developing an android app and I need to send a message to specific contact from WhatsApp. I tried this code:

Uri mUri = Uri.parse(\"smsto:+999999999\");         


        
13条回答
  •  遥遥无期
    2020-12-02 18:35

    Here's my way to do it (more here):

    First, if you want to be sure you can send the message, you can check if the person has a WhatsApp account on the address book:

    @RequiresPermission(permission.READ_CONTACTS)
    public String getContactMimeTypeDataId(@NonNull Context context, String contactId, @NonNull String mimeType) {
        if (TextUtils.isEmpty(mimeType) || !PermissionUtil.hasPermissions(context, Manifest.permission.READ_CONTACTS))
            return null;
        ContentResolver cr = context.getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, new String[]{Data._ID}, Data.MIMETYPE + "= ? AND "
                + ContactsContract.Data.CONTACT_ID + "= ?", new String[]{mimeType, contactId}, null);
        if (cursor == null)
            return null;
        if (!cursor.moveToFirst()) {
            cursor.close();
            return null;
        }
        String result = cursor.getString(cursor.getColumnIndex(Data._ID));
        cursor.close();
        return result;
    }
    

    and if all seem well, you open it as if it's from the web:

                final String contactMimeTypeDataId = getContactMimeTypeDataId(context, contactId, "vnd.android.cursor.item/vnd.com.whatsapp.profile");
                if (contactMimeTypeDataId != null) {
                    final String whatsAppPhoneNumber = PhoneNumberHelper.normalizePhone(phoneNumber);
                    String url = "https://api.whatsapp.com/send?phone="+ whatsAppPhoneNumber ;
                    intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
                    intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
                    .setPackage("com.whatsapp");
                    startActivity(intent);
                }
    

    You can also check if WhatsApp is even installed before of all of this (or remove the setPackage and check if any app can handle the Intent) :

            final PackageManager packageManager = context.getPackageManager();
            final ApplicationInfo applicationInfo = packageManager.getApplicationInfo("com.whatsapp", 0);
            if (applicationInfo == null)
               return;
    

    EDIT: about preparing the Intent with the Uri, I think this way is better:

        @JvmStatic
        fun prepareWhatsAppMessageIntent(normalizedPhoneNumber: String?, message: String? = null): Intent {
    //     example url: "https://api.whatsapp.com/send?phone=normalizedPhoneNumber&text=abc"
            val builder = Uri.Builder().scheme("https").authority("api.whatsapp.com").path("send")
            normalizedPhoneNumber?.let { builder.appendQueryParameter("phone", it) }
            message?.let { builder.appendQueryParameter("text", it) }
            return Intent(Intent.ACTION_VIEW, builder.build())
        }
    

    or alternative (based on here):

        fun prepareWhatsAppMessageIntent(normalizedPhoneNumber: String?, message: String? = null): Intent {
    //     example url: "https://wa.me/normalizedPhoneNumber&text=abc"
            val builder = Uri.Builder().scheme("https").authority("wa.me")
            normalizedPhoneNumber?.let { builder.appendPath(it) }
            message?.let { builder.appendQueryParameter("text", it) }
            return Intent(Intent.ACTION_VIEW, builder.build())
        }
    

提交回复
热议问题