NotificationEvent

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

该接口继承自ExtendableEvent接口。

建设者

NotificationEvent() (en-US)

创建一个新NotificationEvent对象。

物产

从其祖先Event继承属性

NotificationEvent.notification (en-US) 只读

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

NotificationEvent.action (en-US) 只读

返回用户单击的通知按钮的字符串 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('/');
  }));
});

规范

Specification
Notifications API Standard
# notificationevent

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

浏览器兼容性

BCD tables only load in the browser