You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何实现点击向任意WhatsApp号码发送消息

Flutter实现点击向用户WhatsApp号码发消息的具体方案

核心思路是通过系统路由调起设备上已安装的WhatsApp客户端,直接跳转至对应号码的聊天窗口,预填充要发送的消息内容,无需对接WhatsApp商业API,普通个人账号即可使用。

实现步骤

  • 添加依赖
    项目中引入url_launcher库用于处理外部应用跳转,在pubspec.yaml中添加配置:

    dependencies:
      flutter:
        sdk: flutter
      url_launcher: ^6.3.0
    

    配置完成后执行flutter pub get完成依赖安装。

  • 平台权限配置
    安卓11+、iOS系统需要配置应用查询白名单,否则无法检测到WhatsApp安装状态:

    1. 安卓端:打开android/app/src/main/AndroidManifest.xml,在根节点<manifest>下添加<queries>配置:
    <queries>
        <package android:name="com.whatsapp" />
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" />
        </intent>
    </queries>
    
    1. iOS端:打开ios/Runner/Info.plist,添加跳转白名单配置:
    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>whatsapp</string>
    </array>
    
  • 封装消息发送方法
    核心逻辑是拼接WhatsApp官方支持的跳转协议,注意提前清洗号码格式、编码消息内容避免特殊字符转义问题:

    import 'package:url_launcher/url_launcher.dart';
    
    Future<void> sendToWhatsApp(String rawPhone, String presetMsg) async {
      // 清洗号码:移除所有非数字字符(+、横杠、空格、括号等)
      final validPhone = rawPhone.replaceAll(RegExp(r'[^0-9]'), '');
      // 对消息内容做Uri编码,兼容中文、特殊符号
      final encodedMsg = Uri.encodeComponent(presetMsg);
      // 拼接跳转地址,wa.me域名兼容性覆盖个人版、商业版WhatsApp
      final jumpUri = Uri.parse('https://wa.me/$validPhone?text=$encodedMsg');
    
      if (await canLaunchUrl(jumpUri)) {
        // 以外部应用模式打开,强制跳转WhatsApp客户端
        await launchUrl(jumpUri, mode: LaunchMode.externalApplication);
      } else {
        throw '未检测到WhatsApp客户端,请先安装后重试';
      }
    }
    
  • 绑定点击触发事件
    和你从API拉取的用户数据结合,在列表项的发送按钮点击事件中调用封装好的方法即可,示例:

    // userList为接口返回的用户数据列表,每个用户对象包含whatsapp号码、昵称等字段
    ListView.builder(
      itemCount: userList.length,
      itemBuilder: (context, index) {
        final currentUser = userList[index];
        return ListTile(
          title: Text(currentUser.userName),
          subtitle: Text(currentUser.whatsappNo),
          trailing: IconButton(
            icon: Icon(Icons.whatsapp, color: Color(0xFF25D366)),
            onPressed: () async {
              try {
                await sendToWhatsApp(
                  currentUser.whatsappNo,
                  '您好${currentUser.userName},这是来自XX应用的消息',
                );
              } catch (err) {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text(err.toString()))
                );
              }
            },
          ),
        );
      },
    )
    

注意事项

  • 号码必须携带国家码,比如中国号码前缀为86、美国为1、印度为91,不要在号码前加本地前缀0,清洗后必须为纯数字,否则会跳转到无效号码页面。
  • 测试必须使用真机,模拟器无法安装WhatsApp客户端,会直接触发报错。
  • 上述方案只能调起客户端预填充消息,需要用户手动点击发送;如果需要后台自动发送消息、发送图片/文件等富媒体内容,需要申请WhatsApp商业平台账号,对接官方付费API,无法通过个人账号实现。

内容的提问来源于stack exchange,提问作者Asif Shaik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 05:36:26