I use BroadcastReceiver
to process some data after user clicks on action button from notification.
So in order to achieve that first I build PendingIntent:
val intent = Intent(context, CustomBroadcastReceiver::class.java).apply {
action = MY_ACTION
// Also put some extras here like notification id.
}
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)
And this is called from Service
which builds and shows the notification:
NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.title))
.setContentText(getString(R.string.text))
.setSmallIcon(R.drawable.ic_notification)
.addAction(actionIcon, actionTitle, pendingIntent)
.setAutoCancel(true)
.build()
Then in CustomBroadcastReceiver I'm cancelling this notification (I have notification ID from intent):
getNotificationManager().cancel(notificationId)
The problem is that notification is not dismissed, it's till on notification drawer.
I saw answers from this question, but it doesn't help - I'm using applicationContext
, notificationId is correct and it's greater than 0.