flutter_sms_platform.dart
2.76 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
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 '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<String> 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) => value ?? 'Error sending sms');
} else {
String _phones = recipients.join(';');
mapData['recipients'] = _phones;
mapData['sendDirect'] = sendDirect;
return _channel
.invokeMethod<String>('sendSMS', mapData)
.then((value) => value ?? 'Error sending sms');
}
}
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() ? '&' : '?';
}