Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter open whatsapp with text message

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.

like image 935
Kitcc Avatar asked Mar 31 '20 10:03

Kitcc


4 Answers

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>
like image 84
Naslausky Avatar answered Nov 13 '22 20:11

Naslausky


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!

like image 24
HoldOffHunger Avatar answered Nov 13 '22 20:11

HoldOffHunger


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"),
        ),
      );
    }
}
like image 35
Rushikesh Tokapure Avatar answered Nov 13 '22 20:11

Rushikesh Tokapure


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);
like image 39
kahan x10 Avatar answered Nov 13 '22 20:11

kahan x10