本文介绍了chrome.notifications.update() 或 create()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 chrome 扩展采用 notification.id,并且:

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

  1. 更新现有通知(如果存在).或
  2. 如果不存在则创建一个新通知.

调用 clear() 然后 create() 并不理想,因为 remove() 的动画在视觉上都很不协调>create() 方法,我想在没有动画的情况下更新.另外,显然,在消失的通知上调用 update() 不会做任何事情.

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?

推荐答案

由于移除了 Chrome 的通知中心,此方法不再适用于除 ChromeOS 之外的任何平台.

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

解决此问题的可能想法包括在通知上使用 requireInteraction: true 标志以完全控制通知生命周期.

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

这篇关于chrome.notifications.update() 或 create()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:36