diff --git a/README.md b/README.md index 48da449..d022beb 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,12 @@ Create a function for sending messages. ``` dart void _sendSMS(String message, List recipents) async { - String _result = await sendSMS(message: message, recipients: recipents) - .catchError((onError) { - print(onError); - }); -print(_result); + SendSMSResult result = await sendSMS( + message: message, + recipients: recipents, + ).catchError((onError) => print(onError)); + + print(result); } ``` @@ -69,11 +70,13 @@ On Android, you can skip the additional dialog with the sendDirect parameter. String message = "This is a test message!"; List recipents = ["1234567890", "5556787676"]; - String _result = await sendSMS(message: message, recipients: recipents, sendDirect: true) - .catchError((onError) { - print(onError); - }); -print(_result); +SendSMSResult result = await sendSMS( + message: message, + recipients: recipents, + sendDirect: true, +).catchError((onError) => print(onError)); + +print(result); ``` NOTE: This also requires the SEND_SMS permission to be added to the AndroidManifest.xml diff --git a/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt b/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt index 25af08c..9b4927c 100644 --- a/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt +++ b/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt @@ -125,7 +125,7 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { } } - result.success("SMS Sent!") + result.success("sent") } private fun sendSMSDialog(result: Result, phones: String, message: String) { @@ -134,6 +134,6 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { intent.putExtra("sms_body", message) intent.putExtra(Intent.EXTRA_TEXT, message) activity?.startActivityForResult(intent, REQUEST_CODE_SEND_SMS) - result.success("SMS Sent!") + result.success("sent") } } diff --git a/example/lib/main.dart b/example/lib/main.dart index d9eb179..5f53e74 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -30,12 +30,27 @@ class _MyAppState extends State { Future _sendSMS(List recipients) async { try { - String _result = await sendSMS( + SendSMSResult _result = await sendSMS( message: _controllerMessage.text, recipients: recipients, sendDirect: sendDirect, ); - setState(() => _message = _result); + setState(() { + switch (_result) { + case SendSMSResult.sent: + _message = 'SMS Sent!'; + break; + case SendSMSResult.failed: + _message = 'Failed'; + break; + case SendSMSResult.cancelled: + _message = 'Cancelled'; + break; + case SendSMSResult.unknownError: + _message = 'Not sent'; + break; + } + }); } catch (error) { setState(() => _message = error.toString()); } diff --git a/lib/flutter_sms.dart b/lib/flutter_sms.dart index b6a61e5..6a962f5 100644 --- a/lib/flutter_sms.dart +++ b/lib/flutter_sms.dart @@ -2,8 +2,10 @@ import 'dart:async'; import 'src/flutter_sms_platform.dart'; +enum SendSMSResult { sent, failed, cancelled, unknownError } + /// Open SMS Dialog on iOS/Android/Web -Future sendSMS({ +Future sendSMS({ required String message, required List recipients, bool sendDirect = false, diff --git a/lib/flutter_sms_web.dart b/lib/flutter_sms_web.dart index 6867b38..bab85d2 100644 --- a/lib/flutter_sms_web.dart +++ b/lib/flutter_sms_web.dart @@ -1,8 +1,8 @@ import 'dart:async'; -import 'package:flutter/material.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; +import 'flutter_sms.dart'; import 'src/flutter_sms_platform.dart'; class FlutterSmsPlugin extends FlutterSmsPlatform { @@ -12,15 +12,15 @@ class FlutterSmsPlugin extends FlutterSmsPlatform { } @override - Future sendSMS({ + Future sendSMS({ required String message, required List recipients, bool sendDirect = false, }) async { bool _messageSent = await FlutterSmsPlatform.instance.launchSmsMulti(recipients, message); - if (_messageSent) return 'Message Sent!'; - return 'Error Sending Message!'; + if (_messageSent) return SendSMSResult.sent; + return SendSMSResult.unknownError; } @override diff --git a/lib/src/flutter_sms_platform.dart b/lib/src/flutter_sms_platform.dart index ff4361e..1df8737 100644 --- a/lib/src/flutter_sms_platform.dart +++ b/lib/src/flutter_sms_platform.dart @@ -6,6 +6,7 @@ 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'); @@ -34,7 +35,7 @@ class FlutterSmsPlatform extends PlatformInterface { /// /// - Future sendSMS({ + Future sendSMS({ required String message, required List recipients, bool sendDirect = false, @@ -43,16 +44,30 @@ class FlutterSmsPlatform extends PlatformInterface { mapData['message'] = message; if (!kIsWeb && Platform.isIOS) { mapData['recipients'] = recipients; - return _channel - .invokeMethod('sendSMS', mapData) - .then((value) => value ?? 'Error sending sms'); + return _channel.invokeMethod('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('sendSMS', mapData) - .then((value) => value ?? 'Error sending sms'); + return _channel.invokeMethod('sendSMS', mapData).then((value) { + switch (value) { + case 'sent': + return SendSMSResult.sent; + default: + return SendSMSResult.unknownError; + } + }); } }