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

前端 未结 13 1568
梦毁少年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:36

    Great hack Rishabh, thanks a lot, I was looking for this solution since last 3 years.

    As per the Rishabh Maurya's answer above, I have implemented this code which is working fine for both text and image sharing on WhatsApp. I have published this in my android app, so if you want to see it live try my app Bill Book

    Note that in both the cases it opens a whatsapp conversation (if toNumber exists in users whatsapp contact list), but user have to click send button to complete the action. That means it helps in skipping contact selection step.

    For text messages

    String toNumber = "+91 98765 43210"; // contains spaces.
    toNumber = toNumber.replace("+", "").replace(" ", "");
    
    Intent sendIntent = new Intent("android.intent.action.MAIN");
    sendIntent.putExtra("jid", toNumber + "@s.whatsapp.net");
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setPackage("com.whatsapp");
    sendIntent.setType("text/plain");
    startActivity(sendIntent);
    

    For sharing images

    String toNumber = "+91 98765 43210"; // contains spaces.
    toNumber = toNumber.replace("+", "").replace(" ", "");
    
    Intent sendIntent = new Intent("android.intent.action.MAIN");
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
    sendIntent.putExtra("jid", toNumber + "@s.whatsapp.net");
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setPackage("com.whatsapp");
    sendIntent.setType("image/png");
    context.startActivity(sendIntent);
    

    Enjoy WhatsApping!

提交回复
热议问题