18

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.

HK boy
  • 1,398
  • 11
  • 17
  • 25
Kitcc
  • 2,138
  • 4
  • 24
  • 42

8 Answers8

23

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>
Community
  • 1
  • 1
Naslausky
  • 3,443
  • 1
  • 14
  • 24
  • Thanks the above encoding solved the multi-word messages issue. However I am still getting the webpage, even when running the Universal Link that you mention above – Kitcc Mar 31 '20 at 13:06
  • Are you sure you used the "wa.me" domain? When I typed the second part of the answer I copied from your question and got mistaken. I edited my answer. – Naslausky Mar 31 '20 at 14:41
  • Yes I noticed that and used the wa.me. So the code that I'm running now reads: const url = "https://wa.me/?text=Hey buddy, try this super cool new app!"; – Kitcc Mar 31 '20 at 14:43
  • I eddited the answer to include the whatsapp url scheme. Give it a try! – Naslausky Mar 31 '20 at 14:48
  • 2
    Yeah I'd already tried that one. It gives a could not launch error. Thanks for the help. – Kitcc Mar 31 '20 at 15:00
  • 1
    Sorry but...who is receiving this message? I'm using the second option and I need to specify the receiver's phone number, here I see only the text param. – funder7 Apr 09 '21 at 16:36
11

I know it is too late for answering this, but for those who want the same functionality, the current way is to do it like this:

launchUrl(Uri.parse('https://wa.me/$countryCode$contactNo?text=Hi'),
                    mode: LaunchMode.externalApplication);
kahan x10
  • 215
  • 2
  • 12
11

Here is new update way...

whatsapp() async{
   var contact = "+880123232333";
   var androidUrl = "whatsapp://send?phone=$contact&text=Hi, I need some help";
   var iosUrl = "https://wa.me/$contact?text=${Uri.parse('Hi, I need some help')}";
   
   try{
      if(Platform.isIOS){
         await launchUrl(Uri.parse(iosUrl));
      }
      else{
         await launchUrl(Uri.parse(androidUrl));
      }
   } on Exception{
     EasyLoading.showError('WhatsApp is not installed.');
  }
}

and call whatsapp function in onpress or ontap function

Sammrafi
  • 399
  • 4
  • 8
8

till date: 27-06-2022

using package: https://pub.dev/packages/url_launcher

dependencies - url_launcher: ^6.1.2

TextButton(
    onPressed: () {
        _launchWhatsapp();
    },
)

_launchWhatsapp() async {
    var whatsapp = "+91XXXXXXXXXX";
    var whatsappAndroid =Uri.parse("whatsapp://send?phone=$whatsapp&text=hello");
    if (await canLaunchUrl(whatsappAndroid)) {
        await launchUrl(whatsappAndroid);
    } else {
        ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content: Text("WhatsApp is not installed on the device"),
        ),
      );
    }
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 28 '22 at 07:50
5

For using the wa.me domain, make sure to use this format...

https://wa.me/123?text=Your Message here

This will send to the phone number 123. Otherwise, you will get an error message (see? https://wa.me/?text=YourMessageHere ). Or, if you don't want to include the phone number, try this...

https://api.whatsapp.com/send?text=Hello there!

Remember, wa.me requires a phone number, whereas api.whatsapp.com does not. Hope this helps!

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
3

if you will use URL Launcher then the whatsapp link will be open on web browser. So you need to set parameter - not to open on safari browser. The complete code you can find on this flutter tutorial.

But for your case use below code.

await launch(whatappURL, forceSafariVC: false);
Kamal Bunkar
  • 1,354
  • 1
  • 16
  • 20
2

Today i am adding solution its working fine on my desktop and phone

Add 91 if your country code is +91

Remember not add any http or https prefix otherwise wont work.

whatsapp://send?phone=9112345678&text=Hello%20World!

-1

I used an external application for the link and it saved me from errors, here is the code:

  launchUrl(
                  Uri.parse(withPhoneNumberAndText.toString()),
                  mode: LaunchMode.externalApplication,
                );
Pbja1
  • 1
  • 1