【问题标题】:How to send sms with URL_launcher package with flutter?如何用颤振的 URL_launcher 包发送短信?
【发布时间】:2019-01-22 05:46:34
【问题描述】:

你好,我搜索一个简单的例子(Android 和 iOS)用这个包发送短信

https://pub.dartlang.org/packages/url_launcher

在插件页面我只看到如何用电话号码打开短信本机应用程序,但没有额外的消息

sms:<phone number>, e.g. sms:5550101234 Send an SMS message to <phone 
number> using the default messaging app

【问题讨论】:

  • 您无法在没有用户交互的情况下自动发送短信。在 Android 上,您可以使用正文预先填充文本字段,而在 iOS 上则不能。看我的回答。

标签: flutter sms


【解决方案1】:

在 Android 上,支持完整的 sms: URI,您可以发送带有类似正文 (RFC5724) 的消息:

 _textMe() async {
    // Android
    const uri = 'sms:+39 348 060 888?body=hello%20there';
    if (await canLaunch(uri)) {
      await launch(uri);
    } else {
      // iOS
      const uri = 'sms:0039-222-060-888?body=hello%20there';
      if (await canLaunch(uri)) {
        await launch(uri);
      } else {
        throw 'Could not launch $uri';
      }
    }
  }

iOS官方文档说你只能使用URI的数字字段。

正如 Konstantine 指出的那样,如果您使用非标准的 URI 而不是使用 ? 开始查询字符串,而是使用 &amp; 它仍然可以正常工作。这似乎是一个未记录的功能。

短信方案用于启动消息应用程序。 URL 的格式 这种类型是“短信:”,其中是一个可选参数 指定 SMS 消息的目标电话号码。这 参数可以包含数字 0 到 9 以及加号 (+)、连字符 (-) 和句点 (.) 字符。 网址字符串不得包含任何 消息文本或其他信息

PS。要检查平台,您可以使用dart.io library Platform class:

 _textMe() async {
    if (Platform.isAndroid) {
      const uri = 'sms:+39 348 060 888?body=hello%20there';
      await launch(uri);
    } else if (Platform.isIOS) {
      // iOS
      const uri = 'sms:0039-222-060-888&body=hello%20there';
      await launch(uri);
    }
  }

【讨论】:

  • 感谢您的快速回复,您知道如何添加变量号吗?我试过这个,但它不起作用: const uri = 'sms:$Phone_Number?body=hello%20there';其中“Phone_Number”是一个字符串变量
  • @QuentinGuichot 奇怪......它应该可以工作。请参阅this 并尝试使用dartpad
  • 这不是真的。为了在 iOS 上添加一个 body 你只需要替换 ?用 & 符号符号。所以url必须是这样的:sms:898908098&body=text_to_send。在 Android 上,您使用带有问号的普通 URL
  • @Konstantine 这很奇怪,因为在您阅读的官方文档中 * URL 字符串不得包含任何消息文本或其他信息。*。但如果它工作得很好知道。我会尝试并相应地更新我的答案。
  • 我正在尝试这个,但它无法识别正文。就像它正在重定向到短信,但它没有显示正文
【解决方案2】:

你可以在安卓和IOS上试试这个:

sendMessage() async {
    if(Platform.isAndroid){
        //FOR Android
        url ='sms:+6000000000?body=message';
        await launch(url);
    } 
    else if(Platform.isIOS){
        //FOR IOS
        url ='sms:+6000000000&body=message';
    }
}

【讨论】:

    【解决方案3】:

    这个答案是为来这里寻求答案的新人准备的。 以前的答案是正确的,但它们不适用于 iOS。 该应用程序可能会在 iOS 上崩溃,但在 Android 上运行。

    所以要解决我们需要按照下面给出的方式来实现发送短信

      String? encodeQueryParameters(Map<String, String> params) {
        return params.entries
            .map((e) => '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
            .join('&');
      }
      
    Uri smsUri = Uri(
          scheme: 'sms',
          path: '$phoneNumber',
          query: encodeQueryParameters(<String, String>{
            'body':
                'Hey this is message body'
          }),
        );
    
        try {
          if (await canLaunch(smsUri.toString())) {
            await launch(smsUri.toString());
          }
        } catch (e) {
          ScaffoldMessenger.of(context).showSnackBar(
            const SnackBar(
              content: Text('Some error occured'),
            ),
          );
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 2022-11-08
      • 2021-03-25
      • 2021-01-08
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      相关资源
      最近更新 更多