The parameter passed into the onnotificationclick handler, the NotificationEvent interface represents a notification click event that is dispatched on the ServiceWorkerGlobalScope ServiceWorker .

This interface inherits from the ExtendableEvent 接口。

构造函数

NotificationEvent()
创建新的 NotificationEvent 对象。

特性

Inherits properties from its ancestor, 事件 .

NotificationEvent.notification 只读
返回 Notification object representing the notification that was clicked to fire the event.
NotificationEvent.action 只读

Returns the string ID of the notification button the user clicked. This value returns an empty string if the user clicked the notification somewhere other than an action button, or the notification does not have a button.

方法

继承方法来自其父级 ExtendableEvent .

ExtendableEvent.waitUntil()

Extends the lifetime of the event. Tells the browser that work is ongoing.

范例

self.addEventListener('notificationclick', function(event) {
  console.log('On notification click: ', event.notification.tag);
  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(function(clientList) {
    for (var i = 0; i < clientList.length; i++) {
      var client = clientList[i];
      if (client.url == '/' && 'focus' in client)
        return client.focus();
    }
    if (clients.openWindow)
      return clients.openWindow('/');
  }));
});
					

规范

规范 状态 注释
Notifications API
The definition of 'NotificationEvent' in that specification.
实时标准 Living standard.

注意 : This interface is specified in the Notifications API , but accessed through ServiceWorkerGlobalScope .

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
NotificationEvent Chrome 42 Edge ≤18 Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE ? Opera 37 Safari ? WebView Android 不支持 No Chrome Android 42 Firefox Android 44 Opera Android 37 Safari iOS ? Samsung Internet Android 4.0
NotificationEvent() 构造函数 Chrome 42 Edge ≤18 Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE ? Opera 37 Safari ? WebView Android 不支持 No Chrome Android 42 Firefox Android 44 Opera Android 37 Safari iOS ? Samsung Internet Android 4.0
action Chrome 48 Edge ≤18 Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE ? Opera 不支持 No Safari ? WebView Android 不支持 No Chrome Android 48 Firefox Android 44 Opera Android 不支持 No Safari iOS ? Samsung Internet Android 5.0
notification Chrome 42 Edge ≤18 Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE ? Opera 37 Safari ? WebView Android 不支持 No Chrome Android 42 Firefox Android 44 Opera Android 37 Safari iOS ? Samsung Internet Android 4.0

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

元数据

  • 最后修改:
  1. Notifications API
  2. NotificationEvent
  3. 构造函数
    1. NotificationEvent()
  4. 特性
    1. action
    2. notification
  5. Related pages for Web Notifications
    1. Notification
    2. ServiceWorkerGlobalScope.onnotificationclick
    3. ServiceWorkerRegistration.getNotifications()
    4. ServiceWorkerRegistration.showNotification()

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1