diff --git a/.vscode/launch.json b/.vscode/launch.json index 9f8693f..c021031 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "version": "0.2.0", "configurations": [ { - "name": "Flutter", + "name": "Example App", "request": "launch", "type": "dart", "program": "example/lib/main.dart" diff --git a/CHANGELOG.md b/CHANGELOG.md index 33c06fd..5b89349 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.3.3 + +* Adding ability to send SMS directly on Android (#57). + # 2.3.2 * Update Android plugin to use V2 embedding. diff --git a/README.md b/README.md index 134bad3..48da449 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,36 @@ List recipents = ["1234567890", "5556787676"]; _sendSMS(message, recipents); ``` + +## Sending Direct + +**WARNING, there is a narrow category of apps that can get into the play store +using this feature. Using it is only advisable if you fit into this category or +you intent to distribute through a third party platform** + +On Android, you can skip the additional dialog with the sendDirect parameter. + +``` dart +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); +``` + +NOTE: This also requires the SEND_SMS permission to be added to the AndroidManifest.xml + +``` + + + ("message") - val recipients = call.argument("recipients") - sendSMS(result, recipients, message!!) + val message = call.argument("message") ?: "" + val recipients = call.argument("recipients") ?: "" + val sendDirect = call.argument("sendDirect") ?: false + sendSMS(result, recipients, message!!, sendDirect) } "canSendSMS" -> result.success(canSendSMS()) else -> result.notImplemented() @@ -95,7 +100,35 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { return !(activityInfo == null || !activityInfo.exported) } - private fun sendSMS(result: Result, phones: String?, message: String?) { + private fun sendSMS(result: Result, phones: String, message: String, sendDirect: Boolean) { + if (sendDirect) { + sendSMSDirect(result, phones, message); + } + else { + sendSMSDialog(result, phones, message); + } + } + + private fun sendSMSDirect(result: Result, phones: String, message: String) { + // SmsManager is android.telephony + val sentIntent = PendingIntent.getBroadcast(activity, 0, Intent("SMS_SENT_ACTION"), PendingIntent.FLAG_IMMUTABLE) + val mSmsManager = SmsManager.getDefault() + val numbers = phones.split(";") + + for (num in numbers) { + Log.d("Flutter SMS", "msg.length() : " + message.toByteArray().size) + if (message.toByteArray().size > 80) { + val partMessage = mSmsManager.divideMessage(message) + mSmsManager.sendMultipartTextMessage(num, null, partMessage, null, null) + } else { + mSmsManager.sendTextMessage(num, null, message, sentIntent, null) + } + } + + result.success("SMS Sent!") + } + + private fun sendSMSDialog(result: Result, phones: String, message: String) { val intent = Intent(Intent.ACTION_SENDTO) intent.data = Uri.parse("smsto:$phones") intent.putExtra("sms_body", message) diff --git a/example/.flutter-plugins-dependencies b/example/.flutter-plugins-dependencies index 01bacb8..1f87844 100644 --- a/example/.flutter-plugins-dependencies +++ b/example/.flutter-plugins-dependencies @@ -1 +1 @@ -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_sms","path":"/Users/rodydavis/Developer/GitHub/plugins/packages/flutter_sms/","dependencies":["url_launcher"]},{"name":"url_launcher","path":"/usr/local/Caskroom/flutter/1.2.1/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher-6.0.12/","dependencies":[]}],"android":[{"name":"flutter_sms","path":"/Users/rodydavis/Developer/GitHub/plugins/packages/flutter_sms/","dependencies":["url_launcher"]},{"name":"url_launcher","path":"/usr/local/Caskroom/flutter/1.2.1/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher-6.0.12/","dependencies":[]}],"macos":[{"name":"url_launcher_macos","path":"/usr/local/Caskroom/flutter/1.2.1/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher_macos-2.0.2/","dependencies":[]}],"linux":[{"name":"url_launcher_linux","path":"/usr/local/Caskroom/flutter/1.2.1/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher_linux-2.0.2/","dependencies":[]}],"windows":[{"name":"url_launcher_windows","path":"/usr/local/Caskroom/flutter/1.2.1/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher_windows-2.0.2/","dependencies":[]}],"web":[{"name":"flutter_sms","path":"/Users/rodydavis/Developer/GitHub/plugins/packages/flutter_sms/","dependencies":[]},{"name":"url_launcher_web","path":"/usr/local/Caskroom/flutter/1.2.1/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher_web-2.0.4/","dependencies":[]}]},"dependencyGraph":[{"name":"flutter_sms","dependencies":["url_launcher"]},{"name":"url_launcher","dependencies":["url_launcher_linux","url_launcher_macos","url_launcher_web","url_launcher_windows"]},{"name":"url_launcher_linux","dependencies":[]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_web","dependencies":[]},{"name":"url_launcher_windows","dependencies":[]}],"date_created":"2021-09-27 10:56:42.626795","version":"2.5.1"} \ No newline at end of file +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_sms","path":"/Users/dsilk/Development/flutter/packages/flutter_sms/","dependencies":[]},{"name":"url_launcher_ios","path":"/Users/dsilk/.pub-cache/hosted/pub.dartlang.org/url_launcher_ios-6.0.15/","dependencies":[]}],"android":[{"name":"flutter_sms","path":"/Users/dsilk/Development/flutter/packages/flutter_sms/","dependencies":[]},{"name":"url_launcher_android","path":"/Users/dsilk/.pub-cache/hosted/pub.dartlang.org/url_launcher_android-6.0.15/","dependencies":[]}],"macos":[{"name":"url_launcher_macos","path":"/Users/dsilk/.pub-cache/hosted/pub.dartlang.org/url_launcher_macos-3.0.0/","dependencies":[]}],"linux":[{"name":"url_launcher_linux","path":"/Users/dsilk/.pub-cache/hosted/pub.dartlang.org/url_launcher_linux-3.0.0/","dependencies":[]}],"windows":[{"name":"url_launcher_windows","path":"/Users/dsilk/.pub-cache/hosted/pub.dartlang.org/url_launcher_windows-3.0.0/","dependencies":[]}],"web":[{"name":"flutter_sms","path":"/Users/dsilk/Development/flutter/packages/flutter_sms/","dependencies":[]},{"name":"url_launcher_web","path":"/Users/dsilk/.pub-cache/hosted/pub.dartlang.org/url_launcher_web-2.0.8/","dependencies":[]}]},"dependencyGraph":[{"name":"flutter_sms","dependencies":["url_launcher"]},{"name":"url_launcher","dependencies":["url_launcher_android","url_launcher_ios","url_launcher_linux","url_launcher_macos","url_launcher_web","url_launcher_windows"]},{"name":"url_launcher_android","dependencies":[]},{"name":"url_launcher_ios","dependencies":[]},{"name":"url_launcher_linux","dependencies":[]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_web","dependencies":[]},{"name":"url_launcher_windows","dependencies":[]}],"date_created":"2022-02-14 16:50:47.982162","version":"2.10.1"} \ No newline at end of file diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 55ca830..6fb01f8 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -5,8 +5,11 @@ In most cases you can leave this as-is, but you if you want to provide additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. --> + + +