Prevent chrome.notifications API from hiding my notification after a few seconds

后端 未结 5 2122
说谎
说谎 2020-11-28 11:16

I\'m doing around 1-2 notifications a day and it\'s important the user doesn\'t miss it. Is there a way of removing the auto close and only allowing the user to manually cl

5条回答
  •  野性不改
    2020-11-28 11:43

    This answer is obsolete; see this answer for an up to date solution involving requireInteraction flag (Chrome 50+).


    There is a slightly better (but again, hacky) solution.

    When you call update on a notification that changes its priority, and the priority is 0 or above, the notification will be re-shown and the timer for hiding it reset.

    So, you can show a notification with a high priority, (say, 2) and then repeat this on an interval shorter than time to hide the notification:

    chrome.notifications.update(id, {priority : 1}, function(wasUpdated) {
      if(wasUpdated) {
        chrome.notifications.update(id, {priority : 2}, function() {});
      } else {
        // Notification was fully closed; either stop updating or create a new one
      }
    });
    

提交回复
热议问题