NotificationEvent

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

This interface inherits from the ExtendableEvent 接口。

构造函数

NotificationEvent()
创建新的 NotificationEvent 对象。

特性

Inherits properties from its ancestor, Event .

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.

方法

Inherits methods from its parent, 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('/');
  }));
});
					

规范

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

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

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
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

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。