How to send sms with URL_launcher package with flutter?

送分小仙女□ 提交于 2019-12-11 08:57:17

问题


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

回答1:


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';
      if (await canLaunch(uri)) {
        await launch(uri);
      } else {
        throw 'Could not launch $uri';
      }
    }
  }

While on iOS you can only use the number field of The URI.

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:

if (Platform.isAndroid) {

} else if (Platform.isIOS) {

}



回答2:


you can trying this for android and 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';
    }
}


来源:https://stackoverflow.com/questions/54301938/how-to-send-sms-with-url-launcher-package-with-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!