Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notification disappears after showing

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)
    builder.setTicker(tickerText)
            .setContentTitle(contentTitle)
            .setContentText(contentText)
            .setVibrate(vibrate)
            .setSmallIcon(icon)
            .setAutoCancel(true)
            .setLights(-0xff0100, 300, 1000)
            .setSound(uri)
            .setContentIntent(pendingIntent)
            .setStyle(NotificationCompat.BigTextStyle().bigText(contentText))
            .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?

like image 710
khusrav Avatar asked Mar 20 '18 21:03

khusrav


People also ask

Why do some notifications disappear?

Sometimes the notifications will disappear if the notification settings are not correct. This can happen at times, so what you want to do is to enter Settings > Notifications. Here you will find a list of apps that are allowed to send you notifications.

How do I see notifications once they disappear?

In the Settings app locate and tap Notifications. In the resulting window, tap Notification history (Figure 5). Accessing the Notification History from within the Settings app on Android 12.

Why did my notifications disappear on my iPhone?

Check the Notification Settings It is possible to have notifications disappearing from the lock screen if the notification settings are incorrect. To ensure you can see everything you want, the first thing you need to do when you have this issue is head to Settings>>Notifications.


1 Answers

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

 builder.setTicker(tickerText)
        ...
        .setTimeoutAfter(-1)
        ...

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.

like image 181
khusrav Avatar answered Oct 24 '22 21:10

khusrav