This repository was archived by the owner on Jan 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathflutter_sms_platform.dart
More file actions
103 lines (90 loc) · 3.16 KB
/
flutter_sms_platform.dart
File metadata and controls
103 lines (90 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import 'dart:async';
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:url_launcher/url_launcher.dart';
import '../flutter_sms.dart';
import 'user_agent/io.dart' if (dart.library.html) 'user_agent/web.dart';
const MethodChannel _channel = MethodChannel('flutter_sms');
class FlutterSmsPlatform extends PlatformInterface {
/// Constructs a FlutterSmsPlatform.
FlutterSmsPlatform() : super(token: _token);
static final Object _token = Object();
static FlutterSmsPlatform _instance = FlutterSmsPlatform();
/// The default instance of [FlutterSmsPlatform] to use.
///
/// Defaults to [MethodChannelFlutterSmsPlatform].
static FlutterSmsPlatform get instance => _instance;
/// Platform-specific plugins should set this with their own platform-specific
/// class that extends [FlutterSmsPlatform] when they register themselves.
// TODO(amirh): Extract common platform interface logic.
// https://github.com/flutter/flutter/issues/43368
static set instance(FlutterSmsPlatform instance) {
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}
///
///
Future<SendSMSResult> sendSMS({
required String message,
required List<String> recipients,
bool sendDirect = false,
}) {
final mapData = <dynamic, dynamic>{};
mapData['message'] = message;
if (!kIsWeb && Platform.isIOS) {
mapData['recipients'] = recipients;
return _channel.invokeMethod<String>('sendSMS', mapData).then((value) {
switch (value) {
case 'sent':
return SendSMSResult.sent;
case 'cancelled':
return SendSMSResult.cancelled;
case 'failed':
return SendSMSResult.failed;
default:
return SendSMSResult.unknownError;
}
});
} else {
String _phones = recipients.join(';');
mapData['recipients'] = _phones;
mapData['sendDirect'] = sendDirect;
return _channel.invokeMethod<String>('sendSMS', mapData).then((value) {
switch (value) {
case 'sent':
return SendSMSResult.sent;
default:
return SendSMSResult.unknownError;
}
});
}
}
Future<bool> canSendSMS() {
return _channel
.invokeMethod<bool>('canSendSMS')
.then((value) => value ?? false);
}
Future<bool> launchSmsMulti(List<String> numbers, [String? body]) {
if (numbers.length == 1) {
return launchSms(numbers.first, body);
}
String _phones = numbers.join(';');
if (body != null) {
final _body = Uri.encodeComponent(body);
return launch('sms:/open?addresses=$_phones${separator}body=$_body');
}
return launch('sms:/open?addresses=$_phones');
}
Future<bool> launchSms(String? number, [String? body]) {
// ignore: parameter_assignments
number ??= '';
if (body != null) {
final _body = Uri.encodeComponent(body);
return launch('sms:/$number${separator}body=$_body');
}
return launch('sms:/$number');
}
String get separator => isCupertino() ? '&' : '?';
}