語法
js
let updating = browser.notifications.update(
id, // string
options // NotificationOptions
)
引數
id-
string。要更新的通知的 ID。這與notifications.create()的回撥中傳入的 ID 相同。 options-
notifications.NotificationOptions。定義通知的新內容和行為。
返回值
一個 Promise,它將以一個布林值解析:如果通知已更新,則為 true;如果未更新(例如,因為引用的 id 不存在),則為 false。
示例
此示例使用 update() 來更新進度通知。點選瀏覽器操作會顯示通知並啟動一個 alarm,我們用它來更新通知的進度指示器。
請注意,您需要“alarms” 許可權 來建立警報(以及建立通知所需的“notifications”許可權)。另請注意,Firefox 不支援 progress 屬性。
js
let 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.
*/
let CAKE_PREP_INTERVAL = 0.005;
let progress = 0;
browser.alarms.onAlarm.addListener((alarm) => {
progress += 10;
if (progress > 100) {
browser.notifications.clear(cakeNotification);
browser.alarms.clear("cake-progress");
} else {
browser.notifications.update(cakeNotification, { progress });
}
});
browser.browserAction.onClicked.addListener(() => {
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,
});
browser.alarms.create("cake-progress", {
periodInMinutes: CAKE_PREP_INTERVAL,
});
});
});
瀏覽器相容性
載入中…
注意: 此 API 基於 Chromium 的 chrome.notifications API。