PendingIntent.getBoradcast broadcast receiver not called

Viewed 21

I am trying to add a stop button to my foreground service notification, so I used a broadcast receiver. But when I click the stop button in notification nothings happens.

val stopIntent = Intent(this, StopBroadCastReceiver::class.java)
stopIntent.action = STOP_SERVICE
val stopPendingIntent =
    PendingIntent.getBroadcast(applicationContext, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT)

notification
    .setContentIntent(pendingIntent)
    .setSmallIcon(android.R.drawable.ic_dialog_info)
    .setContentTitle("Pyrum is running...")
    .setSubText("192.168.1.67")
    .setOnlyAlertOnce(true)
    .addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.stop), stopPendingIntent)
    .priority = NotificationCompat.PRIORITY_MIN

startForeground(NOTIFICATION_ID, notification.build())

broadcast receiver

inner class StopBroadCastReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        Log.d("hello", "STOP!!!")
    }
}

It should log STOP!!! but it did nothing.

0 Answers
Related