Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ANDROID] Using broadcastreceiver to check if the message is sent or not #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -125,7 +138,6 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
}
}

result.success("SMS Sent!")
}

private fun sendSMSDialog(result: Result, phones: String, message: String) {
Expand All @@ -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
)

}
}
12 changes: 10 additions & 2 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->

<uses-permission android:name="android.permission.SEND_SMS"/>


<uses-permission android:name="android.permission.SEND_SMS" />

<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="smsto"/>
</intent>
</queries>

<application
android:name="${applicationName}"
android:label="example"
Expand Down