Flutter open whatsapp with text message

Issue

I want to open whatsapp from my Flutter application and send a specific text string. I’ll select who I send it to when I’m in whatsapp.

After making some research I came up with this:

_launchWhatsapp() async {
  const url = "https://wa.me/?text=Hey buddy, try this super cool new app!";
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

Which works ok ish, however there are two problems:

  1. As soon as I make the text string into multi words it fails. So if I change it to:
_launchWhatsapp() async {
   const url = "https://wa.me/?text=Hey buddy, try this super cool new app!";
   if (await canLaunch(url)) {
     await launch(url);
   } else {
   throw 'Could not launch $url';
  }
}

Then the Could not launch $url is thrown.

  1. I have whatsapp already installed on my phone, but it doesn’t go directly to the app, instead it gives me a webpage first and the option to open the app.

Here is the webpage that I see:

enter image description here

Any help on resolving either of these issues would be greatly appreciated.

Thanks

Carson

P.s. I’m using the Url_launcher package to do this.

Solution

From the official Whatsapp FAQ, you can see that using "Universal links are the preferred method of linking to a WhatsApp account".

So in your code, the url string should be:

const url = "https://wa.me/?text=YourTextHere";

If the user has Whatsapp installed in his phone, this link will open it directly. That should solve the problem of opening a webpage first.

For the problem of not being able to send multi-word messages, that’s because you need to encode your message as a URL. Thats stated in the documentation aswell:

URL-encodedtext is the URL-encoded pre-filled message.

So, in order to url-encode your message in Dart, you can do it as follows:

const url = "https://wa.me/?text=Your Message here";
var encoded = Uri.encodeFull(url);

As seen in the Dart Language tour.

Please note that in your example-code you have put an extra set of single quotes around the text-message, which you shouldn’t.

Edit:

Another option also presented in the Whatsapp FAQ is to directly use the Whatsapp Scheme. If you want to try that, you can use the following url:

const url = "whatsapp://send?text=Hello World!"

Please also note that if you are testing in iOS9 or greater, the Apple Documentation states:

Important

If your app is linked on or after iOS 9.0, you must declare the URL schemes you pass to this method by adding the LSApplicationQueriesSchemes key to your app’s Info.plist file. This method always returns false for undeclared schemes, whether or not an appropriate app is installed. To learn more about the key, see LSApplicationQueriesSchemes.

So you need to add the following keys to your info.plist, in case you are using the custom whatsapp scheme:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>whatsapp</string>
</array>

Answered By – Naslausky

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.