From 1c58dc2db1ea13ea0ff4e8b99679f6296144d3c0 Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Fri, 28 Oct 2022 17:22:26 +1300 Subject: [PATCH] Local broadcast receiver is not running on stop intent --- .../app/src/main/java/com/massive/AlarmModule.kt | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/java/com/massive/AlarmModule.kt b/android/app/src/main/java/com/massive/AlarmModule.kt index 46bd6c6..341b411 100644 --- a/android/app/src/main/java/com/massive/AlarmModule.kt +++ b/android/app/src/main/java/com/massive/AlarmModule.kt @@ -23,7 +23,7 @@ import com.facebook.react.bridge.ReactMethod import kotlin.math.floor -class AlarmModule internal constructor(context: ReactApplicationContext?) : +class AlarmModule constructor(context: ReactApplicationContext?) : ReactContextBaseJavaModule(context) { var countdownTimer: CountDownTimer? = null @@ -32,17 +32,14 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) : return "AlarmModule" } - private val broadcastReceiver = object : BroadcastReceiver() { + private val receiver = object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { + Log.d("AlarmModule", "Received broadcast intent") Toast.makeText(reactApplicationContext, "called from test receiver", Toast.LENGTH_SHORT) .show() } } - init { - reactApplicationContext.registerReceiver(broadcastReceiver, IntentFilter(STOP_BROADCAST)) - } - @RequiresApi(api = Build.VERSION_CODES.O) @ReactMethod fun add(milliseconds: Int, vibrate: Boolean, sound: String?) { @@ -69,8 +66,7 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) : @ReactMethod fun timer(milliseconds: Int, vibrate: Boolean, sound: String?, noSound: Boolean = false) { Log.d("AlarmModule", "Queue alarm for $milliseconds delay") - val intent = Intent(reactApplicationContext, AlarmModule::class.java) - currentActivity?.startActivityForResult(intent, 0) + reactApplicationContext.registerReceiver(receiver, IntentFilter(STOP_BROADCAST)) val manager = getManager() manager.cancel(NOTIFICATION_ID_DONE) reactApplicationContext.stopService( @@ -166,8 +162,9 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) : val pendingContent = PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_IMMUTABLE) val stopIntent = Intent(STOP_BROADCAST) + stopIntent.flags =Intent.FLAG_ACTIVITY_NEW_TASK val pendingStop = - PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_IMMUTABLE) + PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_MUTABLE) return NotificationCompat.Builder(context, CHANNEL_ID_PENDING) .setSmallIcon(R.drawable.ic_baseline_hourglass_bottom_24).setContentTitle("Resting") .setContentIntent(pendingContent)