Kitcc
Kitcc

Reputation: 3148

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.

Upvotes: 24

Views: 57994

Answers (9)

Jithesh Kumar
Jithesh Kumar

Reputation: 68

Here is new way...

dependencies: 
url_launcher: ^6.2.6




launch(url) async {
    if (await launchUrl(url)) {
      await launchUrl(url);
    } else {
      print("Not supported");
    }   }

ElevatedButton(
  child: Text("Open WhatsApp"),
  onPressed: () => _launch(
      'whatsapp://send?text=sample text&phone=919952313535'),
)

Upvotes: 1

Pbja1
Pbja1

Reputation: 21

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,
                );

Upvotes: 1

Sammrafi
Sammrafi

Reputation: 527

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

Upvotes: 19

Rushikesh Tokapure
Rushikesh Tokapure

Reputation: 139

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"),
        ),
      );
    }
}

Upvotes: 10

kahan x10
kahan x10

Reputation: 285

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);

Upvotes: 13

Parmeet Singh
Parmeet Singh

Reputation: 51

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!

Upvotes: 3

Kamal Bunkar
Kamal Bunkar

Reputation: 1452

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);

Upvotes: 4

HoldOffHunger
HoldOffHunger

Reputation: 20891

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!

Upvotes: 6

Naslausky
Naslausky

Reputation: 3768

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>

Upvotes: 28

Related Questions