From 9f0f336713b33f58dffc26ad99cb8e6aefc703b1 Mon Sep 17 00:00:00 2001 From: Ahmed Elsaadany Date: Wed, 8 Feb 2023 15:21:23 +0200 Subject: [PATCH 1/2] This commit will let us know if the message is sent or not for android. I used android "BroadcastReceiver" to check if the SMS is sent or not. The broadcast receiver will handle all types of message failures in android. - No credit - No service for SMS - Null PDU - Airplane mode turned on --- .../example/flutter_sms/FlutterSmsPlugin.kt | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) 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..93c8308 100644 --- a/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt +++ b/android/src/main/kotlin/com/example/flutter_sms/FlutterSmsPlugin.kt @@ -3,7 +3,10 @@ package com.example.flutter_sms import android.annotation.TargetApi import android.app.Activity import android.app.PendingIntent +import android.content.BroadcastReceiver +import android.content.Context import android.content.Intent +import android.content.IntentFilter import android.content.pm.PackageManager import android.net.Uri import android.os.Build @@ -21,11 +24,14 @@ import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.PluginRegistry.Registrar -class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { +class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware , + BroadcastReceiver() { private lateinit var mChannel: MethodChannel private var activity: Activity? = null private val REQUEST_CODE_SEND_SMS = 205 + var result: Result? = null + override fun onAttachedToActivity(binding: ActivityPluginBinding) { activity = binding.activity } @@ -68,6 +74,8 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { inst.activity = registrar.activity() inst.setupCallbackChannels(registrar.messenger()) } + + const val SENT_SMS_ACTION_NAME = "SMS_SENT_ACTION" } override fun onMethodCall(call: MethodCall, result: Result) { @@ -110,6 +118,11 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { } private fun sendSMSDirect(result: Result, phones: String, message: String) { + this.result = result + val intentFilter = IntentFilter() + intentFilter.addAction(SENT_SMS_ACTION_NAME) + activity?.registerReceiver(this, intentFilter) + // SmsManager is android.telephony val sentIntent = PendingIntent.getBroadcast(activity, 0, Intent("SMS_SENT_ACTION"), PendingIntent.FLAG_IMMUTABLE) val mSmsManager = SmsManager.getDefault() @@ -125,7 +138,6 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { } } - result.success("SMS Sent!") } private fun sendSMSDialog(result: Result, phones: String, message: String) { @@ -136,4 +148,40 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { activity?.startActivityForResult(intent, REQUEST_CODE_SEND_SMS) result.success("SMS Sent!") } + + override fun onReceive(context: Context, intent: Intent) { + if (intent.action.equals(SENT_SMS_ACTION_NAME)) { + when (resultCode) { + Activity.RESULT_OK -> { + result?.success("SMS Sent!"); + } + + 111 -> { + result?.error("111", "RESULT_ERROR_NO_CREDIT", "RESULT_ERROR_NO_CREDIT") + } + + SmsManager.RESULT_ERROR_NO_SERVICE -> { + result?.error("${SmsManager.RESULT_ERROR_NO_SERVICE}", "RESULT_ERROR_NO_SERVICE", "No service for sending SMS") + } + + SmsManager.RESULT_ERROR_NULL_PDU -> { + result?.error("${SmsManager.RESULT_ERROR_NULL_PDU}", "RESULT_ERROR_NULL_PDU", "Null PDU") + + } + + SmsManager.RESULT_ERROR_RADIO_OFF -> { + result?.error("${SmsManager.RESULT_ERROR_RADIO_OFF}", "RESULT_ERROR_RADIO_OFF", "May airplane mode is turned off") + } + + else -> { + result?.error("${SmsManager.RESULT_ERROR_GENERIC_FAILURE}", "RESULT_ERROR_GENERIC_FAILURE", "RESULT_ERROR_GENERIC_FAILURE") + } + } + } + + activity?.unregisterReceiver( + this + ) + + } } From 81639678834fd6a56767703787c7a5481c4f6273 Mon Sep 17 00:00:00 2001 From: Ahmed Elsaadany Date: Thu, 9 Feb 2023 11:06:24 +0200 Subject: [PATCH 2/2] This commit will let us know if the message is sent or not for android. I used android "BroadcastReceiver" to check if the SMS is sent or not. The broadcast receiver will handle all types of message failures in android. - No credit - No service for SMS - Null PDU - Airplane mode turned on --- example/android/app/src/main/AndroidManifest.xml | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 6fb01f8..6fc2986 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -6,8 +6,16 @@ additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. --> - - + + + + + + + + + +