您好,我在寻找一个简单的例子(适用于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),来使用此软件包发送短信。
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 上你只能使用 URI
的电话号码字段。
但正如Konstantine所指出的,如果你使用一个非标准的URI
,并且不是以?
开头的查询字符串,而是用&
代替,它仍然可以正常工作。这似乎是一个未记录在案的功能。
sms方案用于启动“信息”应用。此类URL的格式为“sms:”,其中参数<phone-number>是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);
}
}
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'),
),
);
}
这里是根据设备操作系统发送短信的更新答案。我之前尝试过之前的答案,但在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!'),
),
);
}
}
在Flutter 3和最新的URL Launcher包发布后,更新了最终答案。
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'),
),
);
}