Skip to content

Commit

Permalink
Merge pull request #35 from NdoleStudio/34/enable-multipart-sms-support
Browse files Browse the repository at this point in the history
Enable sending multipart SMS messages. Closes #34
  • Loading branch information
AchoArnold authored Dec 15, 2022
2 parents 51fa2f3 + a9e05be commit 5310c1b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
62 changes: 55 additions & 7 deletions android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,48 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
}

val message = getMessage(applicationContext, messageID) ?: return Result.failure()
val parts = getMessageParts(applicationContext, message)
if (parts.size == 1) {
return handleSingleMessage(applicationContext, message)
}
return handleMultipartMessage(applicationContext, message, parts)
}

registerReceivers(applicationContext, message.id)
private fun handleMultipartMessage(context: Context, message:Message, parts: ArrayList<String>): Result {
registerReceivers(context, message.id)

Timber.d("sending SMS for message with ID [${message.id}]")
return try {
// The trick here is to listen to the intent only on the last part
val sentIntents = ArrayList<PendingIntent>()
val deliveredIntents = ArrayList<PendingIntent>()

for (i in 0 until parts.size) {
var id = "${message.id}.$i"
if (i == parts.size -1) {
id = message.id
}
sentIntents.add(createPendingIntent(id, SmsManagerService.sentAction(id)))
deliveredIntents.add(createPendingIntent(id, SmsManagerService.deliveredAction(id)))
}
SmsManagerService().sendMultipartMessage(this.applicationContext,message.contact, parts, sentIntents, deliveredIntents)
Timber.d("sent SMS for message with ID [${message.id}] in [${parts.size}] parts")
Result.success()
} catch (e: Exception) {
Timber.e(e)
Timber.d("could not send SMS for message with ID [${message.id}] in [${parts.size}] parts")
Result.failure()
}
}


private fun handleSingleMessage(context: Context, message:Message): Result {
registerReceivers(context, message.id)
sendMessage(
message,
createPendingIntent(message, SmsManagerService.sentAction(message.id)),
createPendingIntent(message, SmsManagerService.deliveredAction(message.id))
createPendingIntent(message.id, SmsManagerService.sentAction(message.id)),
createPendingIntent(message.id, SmsManagerService.deliveredAction(message.id))
)

return Result.success()
}

Expand Down Expand Up @@ -149,7 +182,7 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
private fun sendMessage(message: Message, sentIntent: PendingIntent, deliveredIntent: PendingIntent) {
Timber.d("sending SMS for message with ID [${message.id}]")
try {
SmsManagerService().sendMessage(this.applicationContext, message, sentIntent, deliveredIntent)
SmsManagerService().sendTextMessage(this.applicationContext,message.contact, message.content, sentIntent, deliveredIntent)
} catch (e: Exception) {
Timber.e(e)
Timber.d("could not send SMS for message with ID [${message.id}]")
Expand All @@ -158,9 +191,24 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
Timber.d("sent SMS for message with ID [${message.id}]")
}

private fun createPendingIntent(message: Message, action: String): PendingIntent {
private fun getMessageParts(context: Context, message: Message): ArrayList<String> {
Timber.d("getting parts for message with ID [${message.id}]")
return try {
val parts = SmsManagerService().messageParts(context, message.content)
Timber.d("message with ID [${message.id}] has [${parts.size}] parts")
parts
} catch (e: Exception) {
Timber.e(e)
Timber.d("could not get parts message with ID [${message.id}] returning [1] part with entire content")
val list = ArrayList<String>()
list.add(message.content)
list
}
}

private fun createPendingIntent(id: String, action: String): PendingIntent {
val intent = Intent(action)
intent.putExtra(Constants.KEY_MESSAGE_ID, message.id)
intent.putExtra(Constants.KEY_MESSAGE_ID, id)

return PendingIntent.getBroadcast(
this.applicationContext,
Expand Down
14 changes: 11 additions & 3 deletions android/app/src/main/java/com/httpsms/SmsManagerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.os.Build
import android.telephony.SmsManager


class SmsManagerService {
companion object {
private const val ACTION_SMS_SENT = "SMS_SENT"
Expand All @@ -19,9 +20,16 @@ class SmsManagerService {
}
}

fun sendMessage(context: Context, message: Message, sentIntent:PendingIntent, deliveryIntent: PendingIntent) {
getSmsManager(context)
.sendTextMessage(message.contact, null, message.content, sentIntent, deliveryIntent)
fun messageParts(context: Context, content: String): ArrayList<String> {
return getSmsManager(context).divideMessage(content)
}

fun sendMultipartMessage(context: Context, contact: String, parts: ArrayList<String>, sendIntents: ArrayList<PendingIntent>, deliveryIntents: ArrayList<PendingIntent>) {
getSmsManager(context).sendMultipartTextMessage(contact, null, parts, sendIntents, deliveryIntents)
}

fun sendTextMessage(context: Context, contact: String, content: String, sentIntent:PendingIntent, deliveryIntent: PendingIntent) {
getSmsManager(context).sendTextMessage(contact, null, content, sentIntent, deliveryIntent)
}

@Suppress("DEPRECATION")
Expand Down

0 comments on commit 5310c1b

Please sign in to comment.