Sending message through WhatsApp

前端 未结 23 2209
萌比男神i
萌比男神i 2020-11-22 09:42

Since I found some older posts, that tell that whatsapp doesn\'t support this, I was wondering if something had changed and if there is a way to open a whatsapp \'chat\' wit

相关标签:
23条回答
  • 2020-11-22 10:12
    private fun sendWhatsappMessage(phoneNumber:String, text:String) {        
      val url = if (Intent().setPackage("com.whatsapp").resolveActivity(packageManager) != null) {
            "whatsapp://send?text=Hello&phone=$phoneNumber"
        } else {
            "https://api.whatsapp.com/send?phone=$phoneNumber&text=$text"
        }
    
        val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
        startActivity(browserIntent)
    }
    

    This is a much easier way to achieve this. This code checks if whatsapp is installed on the device. If it is installed, it bypasses the system picker and goes to the contact on whatsapp and prefields the text in the chat. If not installed it opens whatsapp link on your web browser.

    0 讨论(0)
  • 2020-11-22 10:15

    This should work whether Whatsapp is installed or not.

    boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
    if (isWhatsappInstalled) {
        Uri uri = Uri.parse("smsto:" + "98*********7")
        Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "Hai Good Morning");
        sendIntent.setPackage("com.whatsapp");
        startActivity(sendIntent);
    } else {
        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
        Uri uri = Uri.parse("market://details?id=com.whatsapp");
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(goToMarket);
    
    }
    
    private boolean whatsappInstalledOrNot(String uri) {
        PackageManager pm = 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-11-22 10:16

    With this code you can open the whatsapp chat with the given number.

    void openWhatsappContact(String number) {
        Uri uri = Uri.parse("smsto:" + number);
        Intent i = new Intent(Intent.ACTION_SENDTO, uri);
        i.setPackage("com.whatsapp");  
        startActivity(Intent.createChooser(i, ""));
    }
    
    0 讨论(0)
  • 2020-11-22 10:16

    You'll want to use a URL in the following format...

    https://api.whatsapp.com/send?text=text
    

    Then you can have it send whatever text you'd like. You also have the option to specify a phone number...

    https://api.whatsapp.com/send?text=text&phone=1234
    

    What you CANNOT DO is use the following:

    https://wa.me/send?text=text
    

    You will get...

    We couldn't find the page you were looking for

    wa.me, though, will work if you supply both a phone number and text. But, for the most part, if you're trying to make a sharing link, you really don't want to indicate the phone number, because you want the user to select someone. In that event, if you don't supply the number and use wa.me as URL, all of your sharing links will fail. Please use app.whatsapp.com.

    0 讨论(0)
  • 2020-11-22 10:17

    Tested on Marshmallow S5 and it works!

        Uri uri = Uri.parse("smsto:" + "phone number with country code");
        Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
        sendIntent.setPackage("com.whatsapp");
        startActivity(sendIntent); 
    

    This will open a direct chat with a person, if whatsapp not installed this will throw exception, if phone number not known to whatsapp they will offer to send invite via sms or simple sms message

    0 讨论(0)
  • 2020-11-22 10:19

    To check if WhatsApp is installed in device and initiate "click to chat" in WhatsApp:

    Kotlin:

    try {
        // Check if whatsapp is installed
        context?.packageManager?.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA)
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://wa.me/$internationalPhoneNumber"))
        startActivity(intent)
    } catch (e: NameNotFoundException) {
        Toast.makeText(context, "WhatsApp not Installed", Toast.LENGTH_SHORT).show()
    }
    

    Java:

    try {
        // Check if whatsapp is installed
        getPackageManager().getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
        Intent intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://wa.me/" + internationalPhoneNumber));
        startActivity(intent);
    } catch (NameNotFoundException e) {
        Toast.makeText(context, "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
    }
    

    getPackageInfo() throws NameNotFoundException if WhatsApp is not installed.

    The internationalPhoneNumber variable is used to access the phone number.

    Reference:

    • https://faq.whatsapp.com/general/chats/how-to-use-click-to-chat?category=5245251
    • https://stackoverflow.com/a/2201999/9636037
    • https://stackoverflow.com/a/15931345/9636037
    0 讨论(0)
提交回复
热议问题