如何发送带有 URL_launcher 包的短信?
How to send sms with URL_launcher package with flutter?
您好,我搜索了一个简单的示例(Android 和 iOS)来使用此包发送短信
https://pub.dartlang.org/packages/url_launcher
在插件页面我只看到如何打开带有 phone 号码的短信本机应用程序,但没有额外的消息
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
而不是使用 ?
开始查询字符串,您使用 &
它仍然有效。这似乎是一个未记录的功能。
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。要检查平台,您可以使用 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);
}
}
您可以为 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'),
),
);
}
最终更新的答案 post 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'),
),
);
}
您好,我搜索了一个简单的示例(Android 和 iOS)来使用此包发送短信
https://pub.dartlang.org/packages/url_launcher
在插件页面我只看到如何打开带有 phone 号码的短信本机应用程序,但没有额外的消息
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
而不是使用 ?
开始查询字符串,您使用 &
它仍然有效。这似乎是一个未记录的功能。
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。要检查平台,您可以使用 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);
}
}
您可以为 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'),
),
);
}
最终更新的答案 post 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'),
),
);
}