fluttersms

How to send sms with URL_launcher package with flutter?


Hello I search a simple example (Android and iOS) to send SMS with this package

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

In the plugin page I only see how to open sms native app with phone number, but no extra message

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

Solution

  • On Android the full sms: URI is supported and you can send a message with a body like that (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';
          }
        }
      }
    

    enter image description here

    On iOS the official doc says you can only use the number field of The URI.

    Instead as Konstantine pointed out, if you use a non standard URI and instead and instead of starting the query string with ? you use & it still works as well. It seems like an undocumented feature.

    The sms scheme is used to launch the Messages app. The format for URLs of this type is “sms:”, where is an optional parameter that specifies the target phone number of the SMS message. This parameter can contain the digits 0 through 9 and the plus (+), hyphen (-), and period (.) characters. The URL string must not include any message text or other information.

    PS. to check the plaform you could use the 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);
        }
      }