Last active
September 14, 2023 16:12
-
-
Save zgramming/791a653d8c98f0fccc0d0c2328821265 to your computer and use it in GitHub Desktop.
Function to send SMS in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.Manifest | |
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.telephony.SmsManager.getSmsManagerForSubscriptionId | |
import android.telephony.SubscriptionManager | |
import android.util.Log | |
import androidx.core.app.ActivityCompat | |
class MethodChannelUtils(private val context: Context) { | |
fun sendSMS( | |
phoneNumber: String, | |
message: String, | |
sim: Int, | |
surveyResponseId: String, | |
): Boolean { | |
val subscriptionManager = context.getSystemService(SubscriptionManager::class.java) | |
if (ActivityCompat.checkSelfPermission(context, | |
Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED | |
) { | |
throw Exception("Permission not granted") | |
} | |
val subscriptionInfo = subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(sim) | |
if (subscriptionInfo != null) { | |
val subscriptionId = subscriptionInfo.subscriptionId | |
val smsManager = getSmsManagerForSubscriptionId(subscriptionId) | |
Log.wtf("METHOD_CHANNEL_UTILS", "Message content: $message") | |
Log.wtf("METHOD_CHANNEL_UTILS", "Message length: ${message.length}") | |
val iSentIntent = Intent(INTENT_SENT_SMS_ACTION) | |
val iDeliveryIntent = Intent(INTENT_DELIVERED_SMS_ACTION) | |
iSentIntent.putExtra("surveyResponseId", surveyResponseId) | |
iDeliveryIntent.putExtra("surveyResponseId", surveyResponseId) | |
val sentPendingIntent = PendingIntent.getBroadcast( | |
context, | |
0, | |
iSentIntent, | |
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE, | |
) | |
// Create a PendingIntent for delivery status | |
val deliveryPendingIntent = PendingIntent.getBroadcast( | |
context, | |
0, | |
iDeliveryIntent, | |
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE, | |
) | |
val length = message.length | |
if (length > 160) { | |
val messageParts = smsManager.divideMessage(message) | |
val sentIntentsArrayList = ArrayList<PendingIntent>() | |
val deliveryIntentsArrayList = ArrayList<PendingIntent>() | |
for (i in messageParts.indices) { | |
sentIntentsArrayList.add(sentPendingIntent) | |
deliveryIntentsArrayList.add(deliveryPendingIntent) | |
} | |
smsManager.sendMultipartTextMessage( | |
phoneNumber, | |
null, | |
messageParts, | |
sentIntentsArrayList, | |
deliveryIntentsArrayList, | |
) | |
} else { | |
smsManager.sendTextMessage( | |
phoneNumber, | |
null, | |
message, | |
sentPendingIntent, | |
deliveryPendingIntent, | |
) | |
} | |
// Register for SMS send action | |
context.registerReceiver(object : BroadcastReceiver() { | |
override fun onReceive(context: Context?, intent: Intent?) { | |
if (intent?.action == INTENT_SENT_SMS_ACTION) { | |
val bundle = intent.extras | |
val surveyResponseIdX = bundle?.getString("surveyResponseId") ?: "" | |
intent.putExtra("surveyResponseId", surveyResponseIdX) | |
} | |
} | |
}, IntentFilter(INTENT_SENT_SMS_ACTION)) | |
// Register for delivery action | |
context.registerReceiver(object : BroadcastReceiver() { | |
override fun onReceive(context: Context?, intent: Intent?) { | |
if (intent?.action == INTENT_DELIVERED_SMS_ACTION) { | |
val bundle = intent.extras | |
val surveyResponseIdX = bundle?.getString("surveyResponseId") ?: "" | |
intent.putExtra("surveyResponseId", surveyResponseIdX) | |
} | |
} | |
}, IntentFilter("DELIVERED_SMS_ACTION")) | |
return true | |
} else { | |
throw Exception("Sim not found") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment