Notification doesn't go away when calling cancelAll()

Viewed 63

I want my push message to go away when I open the App. Push message is created using:

fun showNotification() {
    val intent = Intent(context, LauncherActivity::class.java)

    val pendingIntent =
        PendingIntent.getActivity(context, REQUEST_CODE, intent, PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)

    val notificationBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notification_received)
        .setContentTitle(context.getString(R.string.push_message_received_title))
        .setContentText(context.getString(R.string.push_message_received_title_text))
        .setAutoCancel(true)
        .setContentIntent(pendingIntent)

    val style = NotificationCompat.InboxStyle().addLine(context.getString(R.string.push_message_received_title_text))
        .setBigContentTitle(context.getString(R.string.push_message_received_title))
        .setSummaryText(context.getString(R.string.push_message_received_title_text))

    notificationBuilder.setStyle(style)
    val notificationManager =
        context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val importance = NotificationManager.IMPORTANCE_HIGH
        val channel = NotificationChannel(CHANNEL_ID, context.getString(R.string.app_name), importance)
        notificationManager.createNotificationChannel(channel)
    }

    val manager = context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager?
    val notifications = manager?.activeNotifications
    if (notifications != null && notifications.isNotEmpty()) {
        for (notification in notifications) {
            if (notification.id != NOTIFICATION_ID) {
                notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build())
            }
        }
    } else {
        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build())
    }
}

When I open the App (not via the push message), but press on the app icon, onStart is triggered and I want to remove the push notification from the Notification center:

class LauncherActivity : BaseActivity() {

    override fun onStart() {
        super.onStart()
        // NotificationManagerCompat.from(applicationContext).cancel(NOTIFICATION_ID)
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.cancel(NOTIFICATION_ID)
    }
}

But this code doesn't work. I tried cancelAll(), cancel(NOTIFICATION_ID) etc.

0 Answers
Related