Home » Android » android – notification disappears after showing

android – notification disappears after showing

Posted by: admin May 14, 2020 Leave a comment


We have code similar to the following in our app

    val pendingIntent = PendingIntent.getActivity(ctx, id.toInt(), intent, PendingIntent.FLAG_CANCEL_CURRENT)
    val builder = NotificationCompat.Builder(ctx, Channel.TEST_CHANNEL.channelId)
            .setLights(-0xff0100, 300, 1000)
            .addAction(R.drawable.ic_notification, ctx.getString(R.string.notification), piAction)

    val notification = builder.build()
    val nf = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    nf.notify(NOTIFICATION_TAG, id.toInt(), notification)

Starting recently we noticed that notifications on some device running Android 8+ started disappearing briefly after being shown, without user’s interaction. Setting auto-cancel to false helps, but the user experience degrades.

The id is a unique item id from the database. This may be important thing to note – technically we can have a notification with such id be shown, removed/canceleld by user, and later some time used again for a similar notification with the same id. Can this be the reason?

How to&Answers:

We’ve updated the support libs and tried the following method on builder for luck:


Setting this param to a positive value delayed the notification disappearing by that amount of time (so it did affect). Thus we tried a negative number, the notifications seem to stay there now.

I couldn’t find any reasonable documentation explaining this, so this answer is not 100%, but keeping it here for now for others to try and see if it helps them.


Disable your application from auto optimize from battery optimization setting in android OREO. Notification will stay as long as you want


Only thing I found uncertain is NotificationCompat.Builder

Android oreo now uses Notification.Builder instead of NotificationCompat.Builder.

Might be you have to check android version like:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
         //Use Notification.Builder         
         } else {
         // Use NotificationCompat.Builder.

I don’t think unique id will be an issue for disappearing notification.

Google has created open source sample for this new changes. Please refer to it for more info.




May be it will work for you.