20

I am building an app that has to be able to take an order and send it to a specific WhatsApp number. What exactly am I supposed to do? I can open WhatsApp but I can't figure out how to send a message when opening it.

 title: new Text("WhatsApp"),
            trailing: new Icon(Icons.message),
             onTap: () async {
              int phone = 962770593839;
              var whatsappUrl = "whatsapp://send?phone=$phone";
              await UrlLauncher.canLaunch(whatsappUrl) != null
                  ? UrlLauncher.launch(whatsappUrl)
                  : print(
                      "open WhatsApp app link or do a snackbar with 
notification that there is no WhatsApp installed");
            },

I expect that when I input a TextField and press send that saved String will be able to be sent to the WhatsApp number after launching WhatsApp.

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
Unison Coding
  • 309
  • 1
  • 2
  • 3
  • That's not quite how apps work. You can either start a share intent so apps supporting that can use it, or you can start a deep link URL, which you're using here. A deep link is sort of an API local to the phone that the apps ship with, and it depends totally on WhatsApp, on how they've implemented it. Check WhatsApp docs for more information on how to use the deep links. – ishaan Apr 28 '19 at 17:13
  • In flutter we don't have Intent like i Android. I can easily send messages from my App to WhatsApp in Android. Why could i do it here.? i Can launch whatsapp but cant send anything. – Unison Coding Apr 28 '19 at 19:25
  • Great! you know how to do in Android, just write your own [plugin](https://flutter.dev/docs/development/packages-and-plugins/developing-packages), and It could be done as what you did in Android. – Tokenyet Sep 21 '19 at 15:53
  • aha. I am new to flutter but old in android native. can you help me with some questions in flutter like is it possible to use the accessibility service as we use in android also like your question about WhatsApp intent? does this solve our problem with these requirements? or just stick with native development for this kind of use – Gulab Sagevadiya Apr 14 '22 at 08:54

7 Answers7

30

Use the plugin.

url_launcher

Using the following link : https://api.whatsapp.com/send?phone=XXXXXXXXXXX (In place of the Xs type the phone number of the person you want to contact, including the country code, but without the + sign.)

RaisedButton(
      onPressed: () async => await launch(
         "https://wa.me/${number}?text=Hello"),,
      child: Text('Open Whatsapp'),
),

Alternatively

You can use this other plugin.

whatsapp_unilink

The whatsapp_unilink package helps you build HTTP links and provides you with an idiomatic Dart interface that:

import 'package:whatsapp_unilink/whatsapp_unilink.dart';
import 'package:url_launcher/url_launcher.dart';

launchWhatsApp() async {
  final link = WhatsAppUnilink(
    phoneNumber: '+001-(555)1234567',
    text: "Hey! I'm inquiring about the apartment listing",
  );
  await launch('$link');
}
Steven Ogwal
  • 685
  • 6
  • 8
  • 2
    This fills up the field, but does not press the Send key. Is there a way to automate the whole process including pressing the Send key – Joel G Mathew Jul 02 '21 at 07:18
  • What do you mean by does not press the Send key? launchWhatsApp() is a function. – Steven Ogwal Jul 04 '21 at 09:22
  • Meaning, ultimately a URI is created, and when you `launch` it, Whatsapp window is opened, and the message text pre-filled, but it cannot send the message automatically without a user press of the Send button in the window – Joel G Mathew Jul 04 '21 at 09:46
  • 2
    I get your point now however, WhatsApp user privacy policy doesn't allow sending a message without user consent. – Steven Ogwal Jul 05 '21 at 15:14
3

Try flutter_open_whatsapp plugin.You Directly Send Message to Number

FlutterOpenWhatsapp.sendSingleMessage("918179015345", "Hello");

Link Open in WhatsApp

  • 6
    For anyone that might be tempted to use this package, I advise to not use it. It's badly maintained and the author does not respond to the issues raised by users in GitHub. I tried using it anyway, and got a java integer casting error. – MAA Jul 13 '20 at 11:30
  • i'm Also using this – Taimoor Hasan Jul 15 '20 at 03:53
0

You can do it like this.

onPressed: () async {         
          for (var msg in msgList) {
            if (msg["phone"] != null) {
              var url = "${baseURL}91${msg['phone']}&text=${msg['messages']}";
              print(url);
              AndroidIntent intent = AndroidIntent(
                  action: 'action_view',
                  data: Uri.encodeFull(url),
                 package: "com.whatsapp.w4b");
              intent.launch();
            }
          }
        },
        child: Icon(Icons.send),
      ),
sneha
  • 92
  • 12
0

Use whatsapp_share package

This launches whatsApp with respective number and prefills the text field.

  • for text and link
 Future<void> share() async {
    await WhatsappShare.share(
      text: 'Whatsapp share text',
      linkUrl: 'https://flutter.dev/',
      phone: '911234567890',
    );
  }
  • Share images, files

_image.path is the file path which you wanna share to whatsapp

 Future<void> shareFile() async {
    await WhatsappShare.shareFile(
      text: 'Whatsapp share text',
      phone: '911234567890',
      filePath: "${_image.path}",
    );
  }

Complete example here

Amit Patil
  • 11
  • 1
0

I use url_launcher: ^6.1.7 like this.

void launchWhatsapp(
  String phone,
  String message,
) async {
  final url = 'https://wa.me/967$phone?text=$message';

  await launchUrlString(
    url,
    mode: LaunchMode.externalApplication,
  );
}

what I want to refer here is the launch model default set to

LaunchMode.platformDefault

and that opens a web page and not WhatsApp when I try to launch WhatsApp

so set the launch model to

model: LaunchMode.externalApplication

has fixed the issue

0
launchWhatsApp() async {
int phone =XXXXXXXXX;
var whatsappUrl = "whatsapp://send?phone=$phone";await launchUrl(Uri.parse(whatsappUrl));}
eglease
  • 2,445
  • 11
  • 18
  • 28
0

Latest Answer: 2023

The latest version of doesn't support launch use launchUrl instead. And you may run into issues using https:... use whatsapp:... instead

Complete code:

FilledButton(
   onPressed: () async {
     var whatsappUrl = Uri.parse(
                     "whatsapp://send?phone=${countryCodeText + numberText}" +
                     "&text=${Uri.encodeComponent("Your Message Here")}");
     try {
       launchUrl(whatsappUrl);
     } catch (e) {
       debugPrint(e.toString());
     }},
  child: const Text("Send Message")
);
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88