如何使用 UrlLauncher 直接从我的 Flutter 应用程序向 WhatsApp 发送消息?

Uni*_*ing 11 dart flutter flutter-dependencies

我正在构建一个应用程序,它必须能够接受订单并将其发送到特定的 WhatsApp 号码。我到底该怎么做?我可以打开 WhatsApp,但在打开它时我不知道如何发送消息。

 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");
            },
Run Code Online (Sandbox Code Playgroud)

我希望当我输入一个 TextField 并按下发送时,保存的字符串将能够在启动 WhatsApp 后发送到 WhatsApp 号码。

Ste*_*wal 12

使用插件。

url_launcher

使用以下链接:https : //api.whatsapp.com/send?phone =XXXXXXXXXXX(在 X 的位置键入您要联系的人的电话号码,包括国家/地区代码,但不带 + 号。)

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

或者

您可以使用这个其他插件。

whatsapp_unilink

whatsapp_unilink 包可帮助您构建 HTTP 链接并为您提供一个惯用的 Dart 接口:

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');
}
Run Code Online (Sandbox Code Playgroud)

  • 这会填充该字段,但不会按发送键。有没有办法自动化整个过程,包括按发送键 (2认同)
  • 我现在明白你的意思了,不过,WhatsApp 用户隐私政策不允许在未经用户同意的情况下发送消息。 (2认同)