Anthony
Anthony

Reputation: 7828

How to ensure a notification peeks (not heads-up)

I have a notification that is popped up during a I/O task that I'd like to peek so the user knows the details are elsewhere (the status bar). On older devices, my code works, but on newer devices the notification does not dismiss to the status bar, it becomes a permanent heads-up. I expect that on newer devices the notification update (current file) causes the peek to reset. Anyone know of a way to ensure the notification is a peek, not a heads-up?

My app is full-screen, which is why a default priority peeks.

    Util.createNotificationChannel(
        applicationContext,
        "copy",
        "Copying...",
        "Notifications for copy tasks.")

    val builder = Util.createNotification(
        applicationContext,
        "copy",
        applicationContext.getString(R.string.copyingImages),
        applicationContext.getString(R.string.preparing))

    val notifications = NotificationManagerCompat.from(applicationContext)
    notifications.notify(builder.build())

    ...
    (loop)

    builder
        .setProgress(images.size, index, false)
        .setContentText(value.name)
        .priority = NotificationCompat.PRIORITY_DEFAULT
    notifications.notify(builder.build())

Upvotes: 1

Views: 433

Answers (2)

Anthony
Anthony

Reputation: 7828

@paulajcm had the core of it:

I believe Notification.Builder.setOnlyAlertOnce() will have the result you expect.

The other aspect to take into consideration is that the notification id must be unique or you'll never peek for that type of notification again since you've effectively set that id to not peek again.

builder.notify(JOB_TAG, id, notification) // id must be 'unique'

The other thing I did was toggle setOnlyAlertOnce(false) for the final message, so there's a peek to notify the operation is complete as well:

        builder
            .setContentText("Complete")
            .setProgress(0,0,false)
            .setOnlyAlertOnce(false)
        notifications.notify(builder.build(), notificationId)

So now it's clear to a user when a long-running background task has started and ended, without bugging them in between.

Upvotes: 1

paulajcm
paulajcm

Reputation: 202

I believe Notification.Builder.setOnlyAlertOnce() will have the result you expect.

Upvotes: 1

Related Questions