NotificationEvent

翻译正在进行中。

传递给 onnotificationclick 处理程序的参数的NotificationEvent接口,该接口表示通知单击事件,该事件在 ServiceWorkerGlobalScopeServiceWorker

该接口继承自ExtendableEvent接口。

建设者

NotificationEvent()
创建一个新NotificationEvent对象。

物产

从其祖先Event继承属性

NotificationEvent.notification 只读
返回一个Notification对象,该对象表示单击以触发事件的通知。
NotificationEvent.action 只读
返回用户单击的通知按钮的字符串ID。如果用户在除操作按钮之外的其他位置单击了通知,或者该通知没有按钮,则此值返回一个空字符串。

方法

从其父项ExtendableEvent继承方法

ExtendableEvent.waitUntil()

延长事件的寿命。告诉浏览器工作正在进行中。

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
NotificationEvent
Living Standard 生活水平。

注意:此接口在Notifications API中指定,但是可以通过 ServiceWorkerGlobalScope访问。

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
NotificationEvent
Experimental
Chrome Full support 42Edge Full support ≤18Firefox Full support 44
Notes
Full support 44
Notes
Notes Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE No support NoOpera Full support 37Safari ? WebView Android No support NoChrome Android Full support 42Firefox Android Full support 44Opera Android Full support 37Safari iOS ? Samsung Internet Android Full support 4.0
NotificationEvent() constructor
Experimental
Chrome Full support 42Edge Full support ≤18Firefox Full support 44
Notes
Full support 44
Notes
Notes Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE No support NoOpera Full support 37Safari ? WebView Android No support NoChrome Android Full support 42Firefox Android Full support 44Opera Android Full support 37Safari iOS ? Samsung Internet Android Full support 4.0
action
Experimental
Chrome Full support 48Edge Full support ≤18Firefox Full support 44
Notes
Full support 44
Notes
Notes Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE No support NoOpera No support NoSafari ? WebView Android No support NoChrome Android Full support 48Firefox Android Full support 44Opera Android No support NoSafari iOS ? Samsung Internet Android Full support 5.0
notification
Experimental
Chrome Full support 42Edge Full support ≤18Firefox Full support 44
Notes
Full support 44
Notes
Notes Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE No support NoOpera Full support 37Safari ? WebView Android No support NoChrome Android Full support 42Firefox Android Full support 44Opera Android Full support 37Safari iOS ? Samsung Internet Android Full support 4.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.