ServiceWorkerGlobalScope:notificationclose 事件

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

注意:此功能僅在 Service Workers 中可用。

notificationclose 事件是 ServiceWorkerGlobalScope 介面的一部分,當用戶關閉由 ServiceWorkerRegistration.showNotification() 觸發的已顯示通知時,該事件會被觸發。

在主執行緒或非 Service Worker 的 Worker 中使用 Notification() 建構函式建立的通知,將會在 Notification 物件本身上接收一個 close 事件。

此事件不可取消,也不會冒泡。

語法

在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。

js
addEventListener("notificationclose", (event) => { })

onnotificationclose = (event) => { }

事件型別

一個 NotificationEvent。繼承自 ExtendableEventEvent

Event ExtendableEvent NotificationEvent

事件屬性

繼承自其祖先 ExtendableEventEvent 的屬性.

NotificationEvent.notification 只讀

返回一個 Notification 物件,表示被點選以觸發事件的通知。

NotificationEvent.action 只讀

返回使用者點選的通知按鈕的字串 ID。如果使用者點選了通知中的操作按鈕以外的區域,或者通知沒有按鈕,則此值為空字串。

示例

js
// Inside a service worker.
self.onnotificationclose = (event) => {
  console.log("On notification close: ", event.notification.tag);
};

規範

規範
Notifications API
# dom-serviceworkerglobalscope-onnotificationclose

瀏覽器相容性