10

I'm developing a hybrid app for android. I've added the following code:

<a href="whatsapp://send data-text=mytext">Send message to WhatsApp</a>
<a href="whatsapp://send?text=Hello%20World!">Hello, world!</a>

However, when I click on those links on my phone, whatsapp doesn't pop-up. Can you, please, help me out? Why isn't that message being shared?

Thanks in advance!

Pablo D.
  • 172
  • 1
  • 2
  • 11

2 Answers2

15

This is the URL you should use:

https://api.whatsapp.com/send?phone=15551234567&text=I'm%20interested%20in%20your%20car%20for%20sale

In JavaScript, you must encode the text for e.g.

var url='https://api.whatsapp.com/send'
var text='text can contain this char: &'
window.open(url + '?phone=2211227373&text=' encodeURIComponent(text))
Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
0

Actually I use this code in Flutter:

onPressed: () async {
  String appUrl;
  String phone = '+989125272xxx'; // phone number to send the message to
  String message = 'Merhaba,'; // message to send
  if (Platform.isAndroid) {
    appUrl = "whatsapp://send?phone=$phone&text=${Uri.parse(message)}"; // URL for Android devices
  } else {
    appUrl = "https://api.whatsapp.com/send?phone=$phone=${Uri.parse(message)}"; // URL for non-Android devices
  }

  // check if the URL can be launched
  if (await canLaunchUrl(Uri.parse(appUrl))) {
    // launch the URL
    await launchUrl(Uri.parse(appUrl));
  } else {
    // throw an error if the URL cannot be launched
    throw 'Could not launch $appUrl';
  }
},

but it is very important, you need to add the QUERY_ALL_PACKAGES permission to work fine on Android 12 or higher devices to your app's AndroidManifest.xml file. This permission allows your app to query all installed packages on the device, including WhatsApp.

Add the following line inside the manifest tag in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
Amir Hosseinzadeh
  • 7,360
  • 4
  • 18
  • 33