Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send sms with URL_launcher package with flutter?

Tags:

flutter

sms

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
like image 408
Nitneuq Avatar asked Jan 22 '19 05:01

Nitneuq


People also ask

How do I send a text message Flutter?

To send an SMS in the Flutter application, we can use the package provided by Flutter named flutter_sms. This will add the following line in our pubspec. yaml file indicating that the package has been successfully installed.

How do you send MMS on Flutter?

To use SMS & MMS functionality in Flutter we need to add the dependency package to pubspec. yaml file. use the below code to add dependency package. After adding the dependency package run the get package method to import all the required files to the app.


1 Answers

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);
    }
  }
like image 72
shadowsheep Avatar answered Oct 05 '22 11:10

shadowsheep