notifications.create() 编辑

Creates and displays a notification.

Pass a notifications.NotificationOptions to define the notification's content and behavior.

You can optionally provide an ID for the notification. If you omit the ID, an ID will be generated. You can use the ID to update or clear the notification.

This is an asynchronous function that returns a Promise.

If you call notifications.create() more than once in rapid succession, Firefox may end up not displaying any notification at all.

Syntax

var creating = browser.notifications.create(
  id,                   // optional string
  options               // NotificationOptions
)

Parameters

idOptional
string. This is used to refer to this notification in notifications.update(), notifications.clear(), and event listeners. If you omit this argument or pass an empty string, then a new ID will be generated for this notification. If the ID you provide matches the ID of an existing notification from this extension, then the other notification will be cleared.
options
notifications.NotificationOptions. Defines the notification's content and behavior.

Return value

A Promise that will be fulfilled when the notification is created and the display process has been started, which is before the notification is actually displayed to the user. It is fulfilled with a string representing the notification's ID.

Browser compatibility

BCD tables only load in the browser

Examples

Create and display a basic notification periodically, using an alarm. Clicking the browser action dismisses the notification.

Note that you'll need the "alarms" permission to create alarms (as well as the "notifications" permission to create notifications).

var cakeNotification = "cake-notification"

/*

CAKE_INTERVAL is set to 6 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_INTERVAL = 0.1;

browser.alarms.create("", {periodInMinutes: CAKE_INTERVAL});

browser.alarms.onAlarm.addListener(function(alarm) {
  browser.notifications.create(cakeNotification, {
    "type": "basic",
    "iconUrl": browser.runtime.getURL("icons/cake-96.png"),
    "title": "Time for cake!",
    "message": "Something something cake"
  });
});

browser.browserAction.onClicked.addListener(()=> {
  var clearing = browser.notifications.clear(cakeNotification);
  clearing.then(() => {
    console.log("cleared");
  });
});

Display a similar notification, but add buttons naming cakes, and log the selected cake when a button is clicked:

var cakeNotification = "cake-notification"

/*

CAKE_INTERVAL is set to 6 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_INTERVAL = 0.1;

var buttons = [
  {
    "title": "Chocolate"
  }, {
    "title": "Battenberg"
  }
];

browser.alarms.create("", {periodInMinutes: CAKE_INTERVAL});

browser.alarms.onAlarm.addListener(function(alarm) {
  browser.notifications.create(cakeNotification, {
    "type": "basic",
    "iconUrl": browser.runtime.getURL("icons/cake-96.png"),
    "title": "Time for cake!",
    "message": "Something something cake",
    "buttons": buttons
  });
});

browser.browserAction.onClicked.addListener(()=> {
  var clearing = browser.notifications.clear(cakeNotification);
  clearing.then(() => {
    console.log("cleared");
  });
});

browser.notifications.onButtonClicked.addListener((id, index) => {
  browser.notifications.clear(id);
  console.log("You chose: " + buttons[index].title);
});

Example extensions

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技术交流群

发布评论

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

词条统计

浏览:79 次

字数:7137

最后编辑:6 年前

编辑次数:0 次

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