notifications.getAll()

獲取擴充套件程式建立的所有當前活動的通知。

這是一個非同步函式,返回一個 Promise

語法

js
let gettingAll = browser.notifications.getAll()

引數

無。

返回值

一個 Promise,它將以一個物件的形式被 fulfilled。每個當前活動的通知都是該物件的一個屬性:屬性的名稱是通知的 ID,屬性的值是一個 notifications.NotificationOptions 物件,描述該通知。

請注意,您可以透過在 notifications.create() 中傳遞 ID 來顯式定義通知的 ID。如果您不這樣做,瀏覽器將生成一個 ID。顯式指定的 ID 是字串,而生成的 ID 是數字。

示例

此示例顯示了當使用者點選瀏覽器操作時的一個通知,除非該通知已在顯示中,在這種情況下,它會清除該通知。它使用 getAll() 來確定通知是否正在顯示。

js
const myNotification = "my-notification";

function toggleAlarm(all) {
  const ids = Object.keys(all);
  if (ids.includes(myNotification)) {
    browser.notifications.clear(myNotification);
  } else {
    console.log("showing");

    browser.notifications.create(myNotification, {
      type: "basic",
      title: "Am imposing title",
      message: "Some interesting content",
    });
  }
}

function handleClick() {
  console.log("clicked");
  browser.notifications.getAll().then(toggleAlarm);
}

browser.browserAction.onClicked.addListener(handleClick);

此示例記錄所有活動通知的標題。

js
function logNotifications(all) {
  for (const id in all) {
    console.log(`Title: ${all[id].title}`);
  }
}

browser.notifications.getAll().then(logNotifications);

瀏覽器相容性

注意: 此 API 基於 Chromium 的 chrome.notifications API。