我想从我的 flutter 应用程序启动 WhatsApp 应用程序

问题描述 投票:0回答:7

我在我的项目中使用此依赖项 url_launcher: ^5.4.1 通过我的 flutter 应用程序启动 Whatsapp,但是当我按下按钮启动应用程序时,它不起作用,但在模拟器上显示无法打开链接的错误消息。 下面给出的是我用来启动 Whatsapp 的函数代码。

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io';


void main() => runApp(Wapp());

class Wapp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
          primarySwatch: Colors.orange,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
 void launchWhatsApp(
    {@required int phone,
    @required String message,
    }) async {
  String url() {
    if (Platform.isAndroid) {
      return "whatsapp://wa.me/$phone:03452121308:/?text=${Uri.parse(message)}";
    } else {
      return "whatsapp://send?   phone=$phone&text=${Uri.parse(message)}";
    }
  }

  if (await canLaunch(url())) {
    await launch(url());
  } else {
    throw 'Could not launch ${url()}';
  }
}


Widget build(BuildContext context){
  return Scaffold(
    appBar: AppBar(
      title: Text("Home"), 
    ),

  body: Center(
    child: RaisedButton(
      
      color: Colors.orange,
      textColor: Colors.black,
      padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 60.0),
      highlightColor: Colors.green,
      onPressed: () {
        launchWhatsApp(phone: 03452121308, message: 'Hello');
      },
      child: Text("Place Your Order",style: TextStyle(
          
          fontWeight: FontWeight.bold,
          fontSize: 15
          
        )
      )
    )
  )

  );
}

}
android flutter dart whatsapp launcher
7个回答
22
投票

您的

https://
中缺少
url

将下面的代码替换为您的

url()
方法:

  String url() {
    if (Platform.isAndroid) {
      // add the [https]
      return "https://wa.me/$phone/?text=${Uri.parse(message)}"; // new line
    } else {
      // add the [https]
      return "https://api.whatsapp.com/send?phone=$phone=${Uri.parse(message)}"; // new line
    }
  }

14
投票

这对我有用,将此代码添加到

android/app/src/main/AndroidManifest.xml
下面的
</Application>
标签:

<queries>
        <!-- If your app opens https URLs -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" />
        </intent>
        <!-- If your app makes calls -->
        <intent>
            <action android:name="android.intent.action.DIAL" />
            <data android:scheme="tel" />
        </intent>
        <!-- If your sends SMS messages -->
        <intent>
            <action android:name="android.intent.action.SENDTO" />
            <data android:scheme="smsto" />
        </intent>
        <!-- If your app sends emails -->
        <intent>
            <action android:name="android.intent.action.SEND" />
            <data android:mimeType="*/*" />
        </intent>
    </queries>

我用来打开Whatsapp的方法:

void openWhatsapp(
      {required BuildContext context,
      required String text,
      required String number}) async {
    var whatsapp = number; //+92xx enter like this
    var whatsappURlAndroid =
        "whatsapp://send?phone=" + whatsapp + "&text=$text";
    var whatsappURLIos = "https://wa.me/$whatsapp?text=${Uri.tryParse(text)}";
    if (Platform.isIOS) {
      // for iOS phone only
      if (await canLaunchUrl(Uri.parse(whatsappURLIos))) {
        await launchUrl(Uri.parse(
          whatsappURLIos,
        ));
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
            const SnackBar(content: Text("Whatsapp not installed")));
      }
    } else {
      // android , web
      if (await canLaunchUrl(Uri.parse(whatsappURlAndroid))) {
        await launchUrl(Uri.parse(whatsappURlAndroid));
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
            const SnackBar(content: Text("Whatsapp not installed")));
      }
    }
  }

11
投票

我找到了一个解决方案,将此代码添加到 android/app/src/main/AndroidManifest.xml

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
    <intent>
        <action android:name="android.intent.action.DIAL" />
        <data android:scheme="tel" />
    </intent>
    <intent>
        <action android:name="android.intent.action.SEND" />
        <data android:mimeType="*/*" />
    </intent>
</queries>

8
投票
 String url() {
      if (Platform.isIOS) {
        return "whatsapp://wa.me/$phone/?text=${Uri.encodeFull(message)}";
      } else {
        return "whatsapp://send?phone=$phone&text=${Uri.encodeFull(message)}";
      }
    }

1
投票

有两件事对我有帮助: 电话号码应该是纯号码。 国家代码应该是带有

+
符号的前缀。

喜欢

+14564564562


0
投票

该插件工作正常,但您需要在 android 清单中添加权限,以便从 android 11 开始打开问题:https://github.com/flutter/flutter/issues/90099


0
投票

我想要wattsap的特殊功能

© www.soinside.com 2019 - 2024. All rights reserved.