簡體   English   中英

如何使用 URL_launcher package 和 flutter 發送短信?

[英]How to send sms with URL_launcher package with flutter?

您好,我搜索了一個簡單的示例(Android 和 iOS)以使用此 package 發送短信

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 上,支持完整的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上,官方文檔說您只能使用 The URI的數字字段。

相反,正如Konstantine指出的那樣,如果您使用非標准URI而不是使用? 你使用&它仍然有效。 這似乎是一個未記錄的功能。

短信方案用於啟動消息應用程序。 此類型 URL 的格式為“sms:”,其中是一個可選參數,用於指定 SMS 消息的目標電話號碼。 此參數可以包含數字 0 到 9 以及加號 (+)、連字符 (-) 和句點 (.) 字符。 URL 字符串不得包含任何消息文本或其他信息

PS。 要檢查平台,您可以使用dart.io 庫Platform

 _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);
    }
  }

你可以在 android 和 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';
    }
}

這個答案是為來這里尋求答案的新人准備的。 以前的答案是正確的,但它們不適用於 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'),
        ),
      );
    }

最終更新的答案發布 Flutter 3 和最新的 url 啟動器包

smsUri = Uri(scheme: 'sms', path: phoneNumber);

try {
  print(smsUri.toString());
  if (await canLaunchUrl(
    smsUri,
  )) {
    await launchUrl(smsUri);
  }
} catch (e) {
  ScaffoldMessenger.of(context).showSnackBar(
    const SnackBar(
      content: const Text('Some error occured'),
    ),
  );
}

這是根據設備操作系統發送短信的更新答案。 我已經嘗試過之前的答案,但我在 IOS 設備上遇到了正文問題。

_launchSms() async {
    try {
      if (Platform.isAndroid) {
        String uri = 'sms:$phoneNumber?body=${Uri.encodeComponent("Hello there")}';
        await launchUrl(Uri.parse(uri));
      } else if (Platform.isIOS) {
        String uri = 'sms:$phoneNumber&body=${Uri.encodeComponent("Hello there")}';
        await launchUrl(Uri.parse(uri));
      }
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content: Text('Some error occurred. Please try again!'),
        ),
      );
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM