Thursday, March 29, 2018

notification disappears after showing

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)     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?

4 Answers

Answers 1

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.

https://github.com/googlesamples/android-NotificationChannels

Answers 2

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.

Answers 3

For the newest version of Android you have to validate which version are you using:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {         //Use Notification.Builder          Notification notification = new Notification.Builder(Main.this)                     /* Make app open when you click on the notification. */                     .setContentIntent(PendingIntent.getActivity(                             Main.this,                             Main.this.i,                             new Intent(Main.this, Main.class),                             PendingIntent.FLAG_CANCEL_CURRENT))                     .setContentTitle(contentTitle)                     .setAutoCancel(true)                     .setContentText(contentText)                     .setSmallIcon(icon)drawn white.                     //.setColor(Color.RED)                     .build();             final NotificationManager notificationManager =                     (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);             notificationManager.notify(Main.this.i, notification);    } else { // Use NotificationCompat.Builder.     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) } 

You can try replacing your code for that one.

Explanation:

NotificationCompat.Builder This constructor was deprecated in API level 26.1.0. use NotificationCompat.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id.

Notification.Builder your app supports versions of Android as old as API level 4, you can instead use NotificationCompat.Builder, available in the Android Support library.

Answers 4

.setAutoCancel(false)

May be it will work for you.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment