chrome.notifications.update() or create()

后端 未结 2 1531
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 11:53

I want my chrome extension to take a notification.id, and:

  1. Update an existing notification if it does exist. OR
  2. Create a new notification
2条回答
  •  醉梦人生
    2020-12-11 12:02

    Edit: This approach no longer works on any platform except ChromeOS due to the removal of Chrome's Notification Center.

    Possible ideas to work around it include using requireInteraction: true flag on notifications to fully control notification lifetime.


    There is a dirty trick for re-showing a notification. If you change a notification's priority to a higher value, it will be re-shown if it exists.

    function createOrUpdate(id, options, callback) {
      // Try to lower priority to minimal "shown" priority
      chrome.notifications.update(id, {priority: 0}, function(existed) {
        if(existed) {
          var targetPriority = options.priority || 0;
          options.priority = 1;
          // Update with higher priority
          chrome.notifications.update(id, options, function() {
            chrome.notifications.update(id, {priority: targetPriority}, function() {
              callback(true); // Updated
            });
          });
        } else {
          chrome.notifications.create(id, options, function() {
            callback(false); // Created
          });
        }
      });
    }
    

提交回复
热议问题