ServiceWorkerGlobalScope:notificationclose 事件

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

安全上下文: 此项功能仅在一些支持的浏览器安全上下文(HTTPS)中可用。

备注: 此特性仅在 Service Worker 中可用。

ServiceWorkerGlobalScope 接口的 notificationclose 事件在用户关闭由 ServiceWorkerRegistration.showNotification() 方法生成的显示的通知时触发。

在主线程或非 Service Worker 线程中使用 Notification() 构造函数创建的通知则将在 Notification 对象本身触发 close 事件。

此事件不可取消且不会冒泡。

语法

addEventListener() 等方法中使用事件名称,或设置事件处理器属性。

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

onnotificationclose = (event) => {};

事件类型

事件属性

从其祖先 ExtendableEventEvent 继承属性

NotificationEvent.notification 只读

返回一个 Notification 对象,表示单击而触发事件的通知。

NotificationEvent.action 只读

返回用户单击的通知按钮的字符串 ID。如果用户单击通知的操作按钮以外的部分,或者通知没有按钮,则此值返回空字符串。

示例

js
// 在 service worker 内部。
self.onnotificationclose = (event) => {
  console.log("当关闭通知时:", event.notification.tag);
};

规范

Specification
Notifications API
# dom-serviceworkerglobalscope-onnotificationclose

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
notificationclose event

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.