ServiceWorkerGlobalScope: notificationclick event

notificationclick 이벤트는 ServiceWorkerRegistration.showNotification() 에 의해 발생한 시스템 notification 이 클릭되었음을 나타내기 위해 발생된다.

No
Cancelable No
Interface NotificationEvent
Event handler onnotificationclick

Examples

addEventListener 메소드 내에서 notificationclick 이벤트를 사용할 수 있다:

js
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("/");
      }),
  );
});

또는 onnotificationclick 이벤트 핸들러 속성을 사용할 수 있다:

js
self.onnotificationclick = 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
# activating-a-notification
Notifications API Standard
# dom-serviceworkerglobalscope-onnotificationclick

브라우저 호환성

BCD tables only load in the browser

See also