簡體   English   中英

如何確保通知通知(不是平視)

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

我有一個想偷看的I / O任務期間彈出的通知,以便用戶知道其他地方(狀態欄)的詳細信息。 在較舊的設備上,我的代碼可以運行,但是在較新的設備上,通知不會在狀態欄上消失,而是永久性提醒。 我希望在更新的設備上,通知更新(當前文件)會導致窺視復位。 有人知道確保通知是窺視而不是平視的方法嗎?

我的應用程序是全屏的,這就是default優先級為的原因

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

我相信Notification.Builder.setOnlyAlertOnce()將獲得您期望的結果。

@paulajcm的核心是:

我相信Notification.Builder.setOnlyAlertOnce()將獲得您期望的結果。

要考慮的另一個方面是,通知ID必須是唯一的,否則您將永遠不會再次窺視該類型的通知,因為您已經有效地將該ID設置為不再窺視。

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

我做的另一件事是將setOnlyAlertOnce(false)切換為最終消息,因此有一個窺視通知操作也已完成:

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

因此,對於一個長期運行的后台任務而言,何時開始和結束對用戶來說是顯而易見的,而不會在它們之間造成錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM