Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

javascript - chrome.notifications.update() or create()

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 if it doesn't exist.

Calling clear() then create() is not ideal, since the animation is visually jarring for both remove() and create() methods, where I want to update without animations. Plus, obviously, calling update() on a disappeared notification doesn't do anything.

Is there an easy way to implement this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
      });
    }
  });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...