notifications.update() 编辑

Updates a notification, given its ID.

This is an asynchronous function that returns a Promise.

Syntax

var updating = browser.notifications.update(
  id,                            // string
  options                        // NotificationOptions
)

Parameters

id
string. The ID of the notification to update. This is the same as the ID passed into notifications.create()'s callback.
options
notifications.NotificationOptions. Defines the notification's new content and behavior.

Return value

A Promise that will be fulfilled with a boolean: true if the notification was updated, or false if it was not (for example, because the notification referenced by id did not exist).

Browser compatibility

BCD tables only load in the browser

Examples

This example uses update() to update a progress notification. Clicking the browser action shows the notification and starts an alarm, which we use to update the notification's progress indicator.

Note that you'll need the "alarms" permission to create alarms (as well as the "notifications" permission to create notifications). Also note that Firefox does not support the progress attribute.

var cakeNotification = "cake-notification";

/*

CAKE_INTERVAL is set to 0.3 seconds in this example.
Such a short period is chosen to make the extension's behavior
more obvious, but this is not recommended in real life.
Note that in Chrome, alarms cannot be set for less than
a minute.

*/
var CAKE_PREP_INTERVAL = 0.005;

var progress = 0;

browser.alarms.onAlarm.addListener(function(alarm) {
  progress = progress + 10;
  if (progress > 100) {
    browser.notifications.clear(cakeNotification);
    browser.alarms.clear("cake-progress");
  } else {
    browser.notifications.update(cakeNotification, {
      "progress": progress
    });
  }
});

browser.browserAction.onClicked.addListener(function () {
  browser.notifications.getAll((all) => {
    if (all.length > 0) {
      browser.notifications.clear(cakeNotification);
      return;
    }
    progress = 0;
    browser.notifications.create(cakeNotification, {
      "type": "progress",
      "iconUrl": browser.extension.getURL("icons/cake-48.png"),
      "title": "Your cake is being prepared...",
      "message": "Something something cake",
      "progress": progress
    });
    browser.alarms.create(
      "cake-progress",
      {periodInMinutes: CAKE_PREP_INTERVAL}
    );
  });
});
Acknowledgements

This API is based on Chromium's chrome.notifications API.

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:45 次

字数:4517

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文