使用扩展程序 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 上创建如下所示的通知。