NotificationEvent:notification 屬性
注意:此功能僅在 Service Workers 中可用。
NotificationEvent 介面的notification 只讀屬性返回在觸發事件時被點選的 Notification 例項。該 Notification 提供了對許多屬性的只讀訪問,這些屬性是在 Notification 例項化時設定的,例如 tag 和 data 屬性,允許你在 notificationclick 事件中儲存資訊以供後續使用。
值
一個 Notification 物件。
示例
js
self.addEventListener("notificationclick", (event) => {
console.log("On notification click");
// Data can be attached to the notification so that you
// can process it in the notificationclick handler.
console.log(`Notification Tag: ${event.notification.tag}`);
console.log(`Notification Data: ${event.notification.data}`);
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(
clients
.matchAll({
type: "window",
})
.then((clientList) => {
for (const client of clientList) {
if (client.url === "/" && "focus" in client) return client.focus();
}
if (clients.openWindow) return clients.openWindow("/");
}),
);
});
規範
| 規範 |
|---|
| Notifications API # dom-notificationevent-notification |
瀏覽器相容性
載入中…