拡張機能の Notifications API を使用して、ユーザーのシステムトレイにメッセージを投稿します。まず、manifest.json で "notifications" 権限を宣言します。
{
"name": "Drink Water Event Popup",
...
"permissions": [
"notifications",
],
...
}
権限が宣言されたら、notifications.create() を呼び出して通知を表示します。次の例は、水を飲むイベントのポップアップのサンプルから抜粋したものです。アラームを使用して、コップ一杯の水を飲むリマインダーを設定します。このコードは、アラームのトリガーを示しています。前のリンクに沿って、この設定方法を確認してください。
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 の通知を作成します。