androidandroid-intentsocial-networkingsharingwhatsapp

Sending message through WhatsApp


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' with a number that I'm sending through an intent?


Solution

  • UPDATE Please refer to https://faq.whatsapp.com/en/android/26000030/?category=5245251

    WhatsApp's Click to Chat feature allows you to begin a chat with someone without having their phone number saved in your phone's address book. As long as you know this person’s phone number, you can create a link that will allow you to start a chat with them.

    Use: https://wa.me/15551234567

    Don't use: https://wa.me/+001-(555)1234567

    Example: https://wa.me/15551234567?text=I'm%20interested%20in%20your%20car%20for%20sale

    Original answer Here is the solution

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

    Also see http://www.whatsapp.com/faq/en/android/28000012