Local broadcast receiver is not running on stop intent

This commit is contained in:
Brandon Presley 2022-10-28 17:22:26 +13:00
parent 8504f8b811
commit 1c58dc2db1
1 changed files with 6 additions and 9 deletions

View File

@ -23,7 +23,7 @@ import com.facebook.react.bridge.ReactMethod
import kotlin.math.floor import kotlin.math.floor
class AlarmModule internal constructor(context: ReactApplicationContext?) : class AlarmModule constructor(context: ReactApplicationContext?) :
ReactContextBaseJavaModule(context) { ReactContextBaseJavaModule(context) {
var countdownTimer: CountDownTimer? = null var countdownTimer: CountDownTimer? = null
@ -32,17 +32,14 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
return "AlarmModule" return "AlarmModule"
} }
private val broadcastReceiver = object : BroadcastReceiver() { private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
Log.d("AlarmModule", "Received broadcast intent")
Toast.makeText(reactApplicationContext, "called from test receiver", Toast.LENGTH_SHORT) Toast.makeText(reactApplicationContext, "called from test receiver", Toast.LENGTH_SHORT)
.show() .show()
} }
} }
init {
reactApplicationContext.registerReceiver(broadcastReceiver, IntentFilter(STOP_BROADCAST))
}
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
@ReactMethod @ReactMethod
fun add(milliseconds: Int, vibrate: Boolean, sound: String?) { fun add(milliseconds: Int, vibrate: Boolean, sound: String?) {
@ -69,8 +66,7 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
@ReactMethod @ReactMethod
fun timer(milliseconds: Int, vibrate: Boolean, sound: String?, noSound: Boolean = false) { fun timer(milliseconds: Int, vibrate: Boolean, sound: String?, noSound: Boolean = false) {
Log.d("AlarmModule", "Queue alarm for $milliseconds delay") Log.d("AlarmModule", "Queue alarm for $milliseconds delay")
val intent = Intent(reactApplicationContext, AlarmModule::class.java) reactApplicationContext.registerReceiver(receiver, IntentFilter(STOP_BROADCAST))
currentActivity?.startActivityForResult(intent, 0)
val manager = getManager() val manager = getManager()
manager.cancel(NOTIFICATION_ID_DONE) manager.cancel(NOTIFICATION_ID_DONE)
reactApplicationContext.stopService( reactApplicationContext.stopService(
@ -166,8 +162,9 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
val pendingContent = val pendingContent =
PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_IMMUTABLE) PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_IMMUTABLE)
val stopIntent = Intent(STOP_BROADCAST) val stopIntent = Intent(STOP_BROADCAST)
stopIntent.flags =Intent.FLAG_ACTIVITY_NEW_TASK
val pendingStop = 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) return NotificationCompat.Builder(context, CHANNEL_ID_PENDING)
.setSmallIcon(R.drawable.ic_baseline_hourglass_bottom_24).setContentTitle("Resting") .setSmallIcon(R.drawable.ic_baseline_hourglass_bottom_24).setContentTitle("Resting")
.setContentIntent(pendingContent) .setContentIntent(pendingContent)