Unison Coding
Unison Coding

Reputation: 349

How to send a message directly from my Flutter app to WhatsApp using UrlLauncher?

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.

Upvotes: 24

Views: 61589

Answers (7)

krishnaacharyaa
krishnaacharyaa

Reputation: 24940

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

Upvotes: 6

saklen khan
saklen khan

Reputation: 11

launchWhatsApp() async {
int phone =XXXXXXXXX;
var whatsappUrl = "whatsapp://send?phone=$phone";await launchUrl(Uri.parse(whatsappUrl));}

Upvotes: 0

Abdullah Alamodi
Abdullah Alamodi

Reputation: 339

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

Upvotes: 2

Amit Patil
Amit Patil

Reputation: 11

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

Upvotes: 0

Steven Ogwal
Steven Ogwal

Reputation: 802

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

Upvotes: 32

sneha
sneha

Reputation: 92

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

Upvotes: 0

Taimoor Hasan
Taimoor Hasan

Reputation: 39

Try flutter_open_whatsapp plugin.You Directly Send Message to Number

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

Link Open in WhatsApp

Upvotes: 3

Related Questions