Skip to content

Commit

Permalink
Fixed the 'Too many receivers, total of 1000, registered' bug
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Sep 10, 2023
1 parent 5d204a3 commit 5b8b883
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
37 changes: 11 additions & 26 deletions android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.httpsms
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.work.*
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
Expand Down Expand Up @@ -140,15 +139,13 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {

val parts = getMessageParts(applicationContext, message)
if (parts.size == 1) {
return handleSingleMessage(applicationContext, message)
return handleSingleMessage(message)
}
return handleMultipartMessage(applicationContext, message, parts)
return handleMultipartMessage(message, parts)
}

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}]")
private fun handleMultipartMessage(message:Message, parts: ArrayList<String>): Result {
Timber.d("sending multipart SMS for message with ID [${message.id}]")
return try {
val sentIntents = ArrayList<PendingIntent>()
val deliveredIntents = ArrayList<PendingIntent>()
Expand All @@ -162,8 +159,8 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
id = message.id
}

sentIntents.add(createPendingIntent(id, SmsManagerService.sentAction(id)))
deliveredIntents.add(createPendingIntent(id, SmsManagerService.deliveredAction(id)))
sentIntents.add(createPendingIntent(id, SmsManagerService.sentAction()))
deliveredIntents.add(createPendingIntent(id, SmsManagerService.deliveredAction()))
}
SmsManagerService().sendMultipartMessage(this.applicationContext,message.contact, parts, message.sim, sentIntents, deliveredIntents)
Timber.d("sent SMS for message with ID [${message.id}] in [${parts.size}] parts")
Expand All @@ -176,27 +173,15 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
}


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

private fun registerReceivers(context: Context, messageID: String) {
context.registerReceiver(
SentReceiver(),
IntentFilter(SmsManagerService.sentAction(messageID))
)
context.registerReceiver(
DeliveredReceiver(),
IntentFilter(SmsManagerService.deliveredAction(messageID))
)
}

private fun handleFailed(context: Context, messageID: String) {
Timber.d("sending failed event for message with ID [${messageID}]")
HttpSmsApiService.create(context)
Expand Down Expand Up @@ -249,9 +234,9 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {

return PendingIntent.getBroadcast(
this.applicationContext,
0,
id.hashCode(),
intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent.FLAG_MUTABLE
)
}
}
Expand Down
25 changes: 25 additions & 0 deletions android/app/src/main/java/com/httpsms/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Build
import android.os.Bundle
Expand Down Expand Up @@ -41,6 +42,9 @@ import android.provider.Settings as ProviderSettings


class MainActivity : AppCompatActivity() {
private var sentReceiver: SentReceiver? = null
private var deliveredReceiver: DeliveredReceiver? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -55,6 +59,7 @@ class MainActivity : AppCompatActivity() {
setCardContent(this)
registerListeners()
refreshToken(this)
registerReceivers(this)

startStickyNotification(this)
scheduleHeartbeatWorker(this)
Expand Down Expand Up @@ -82,6 +87,26 @@ class MainActivity : AppCompatActivity() {
appVersionView.text = format(getString(R.string.app_version), BuildConfig.VERSION_NAME)
}

private fun registerReceivers(context: Context) {
if(sentReceiver == null) {
Timber.d("registering [sent] receiver for intent [${SmsManagerService.sentAction()}]")
sentReceiver = SentReceiver()
context.registerReceiver(
sentReceiver,
IntentFilter(SmsManagerService.sentAction())
)
}

if(deliveredReceiver == null) {
Timber.d("registering [delivered] receiver for intent [${SmsManagerService.deliveredAction()}]")
deliveredReceiver = DeliveredReceiver()
context.registerReceiver(
deliveredReceiver,
IntentFilter(SmsManagerService.deliveredAction())
)
}
}

private fun setCardContent(context: Context) {
val titleText = findViewById<TextView>(R.id.cardPhoneNumber)
titleText.text = PhoneNumberUtils.formatNumber(Settings.getSIM1PhoneNumber(this), Locale.getDefault().country)
Expand Down
8 changes: 4 additions & 4 deletions android/app/src/main/java/com/httpsms/SmsManagerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class SmsManagerService {
private const val ACTION_SMS_SENT = "SMS_SENT"
private const val ACTION_SMS_DELIVERED = "SMS_DELIVERED"

fun sentAction(messageID: String): String {
return "$ACTION_SMS_SENT.$messageID"
fun sentAction(): String {
return "${BuildConfig.APPLICATION_ID}.$ACTION_SMS_SENT"
}

fun deliveredAction(messageID: String): String {
return "$ACTION_SMS_DELIVERED.$messageID"
fun deliveredAction(): String {
return "${BuildConfig.APPLICATION_ID}.$ACTION_SMS_DELIVERED"
}

fun isDualSIM(context: Context) : Boolean {
Expand Down
2 changes: 1 addition & 1 deletion api/cmd/experiments/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {

func loadTest() {
wg := sync.WaitGroup{}
for i := 0; i < 1; i++ {
for i := 0; i < 2; i++ {
wg.Add(1)
go func(count int) {
sendSMS(count)
Expand Down

0 comments on commit 5b8b883

Please sign in to comment.