使用擴充功能 Notifications API 將訊息發布至使用者的系統匣。首先,請在 manifest.json 中宣告 "notifications" 權限。

{
  "name": "Drink Water Event Popup",
...
  "permissions": [
    "notifications",
  ],
...
}

宣告權限後,請呼叫 notifications.create() 來顯示通知。以下範例取自「Drink Water event pop」範例。它會使用鬧鐘來設定喝杯水的提醒。此程式碼會顯示鬧鐘的觸發。請點選上一個連結,瞭解這項設定的設定方式。

chrome.alarms.onAlarm.addListener(() => {
  chrome.action.setBadgeText({ text: '' });
  chrome.notifications.create({
    type: 'basic',
    iconUrl: 'stay_hydrated.png',
    title: 'Time to Hydrate',
    message: "Everyday I'm Guzzlin'!",
    buttons: [{ title: 'Keep it Flowing.' }],
    priority: 0
  });
});

這段程式碼會在 macOS 上建立通知,如下所示。

macOS 中的通知
macOS 上的通知