Android: How to send message programmatically by using WhatsApp, WeChat?

前端 未结 5 1568
情歌与酒
情歌与酒 2020-12-02 17:58

How to use messaging in android application by using WhatsApp and WeChat?

Actually requirement is to send sms using WhatsApp and We

5条回答
  •  囚心锁ツ
    2020-12-02 18:13

    you can try one of these two solutions they worked for me.

    reference: https://faq.whatsapp.com/en/android/26000030/?category=5245251

    private void openWhatsApp(String countryCode, String mobile) {
        String customerPhoneNumber = String.format("%s%s", countryCode, mobile);
        Uri uri = Uri.parse("smsto:" + customerPhoneNumber);
        Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
        sendIntent.setPackage("com.whatsapp");
    
        if(sendIntent.resolveActivity(getPackageManager()) == null){
            showDialogMessage("you should install whatsapp.");
            return;
        }
    
        startActivity(sendIntent);
    }
    
    or
    
    private void openWhatsApp(String countryCode, String mobile) {
        String customerPhoneNumber = String.format("%s%s", countryCode, mobile);
        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.setPackage("com.whatsapp");
    // you can remove this part ("&text=" + "your message")
        String url = "https://api.whatsapp.com/send?phone=" + customerPhoneNumber + "&text=" + "your message"; 
        sendIntent.setData(Uri.parse(url));
    
        if(sendIntent.resolveActivity(getPackageManager()) == null){
            showDialogMessage("you should install whatsapp.");
            return;
        }
    
        startActivity(sendIntent);
    }
    

提交回复
热议问题