Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 274
Unison Coding Avatar asked Apr 28 '19 16:04

Unison Coding


People also ask

How do I send messages on WhatsApp on flutter?

Run Flutter CodeRun the flutter code on a Real android phone and on a real iPhone. The Whatsapp app must be installed on your phone. Flutter code will open whatsapp with a predefined text. If you have any question, please feel free to contact.

How can I send message to WhatsApp API?

Install the WhatsApp Business API Client — Install your API client. Once your client is working, you can update your application settings. Start using the client — Register your phone number with an API call to /account and send a test message with a call to /messages .

How do I send a message on flutter?

To send an SMS in the Flutter application, we can use the package provided by Flutter named flutter_sms. This will add the following line in our pubspec. yaml file indicating that the package has been successfully installed.


1 Answers

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');
}
like image 114
Steven Ogwal Avatar answered Sep 18 '22 17:09

Steven Ogwal