拡張機能の 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 で次のような通知を作成します。