docs.rodeo

MDN Web Docs mirror

ServiceWorkerRegistration: showNotification() method

{{APIRef("Web Notifications")}} {{SecureContext_Header}}  {{AvailableInWorkers}} 

The showNotification() method of the {{domxref("ServiceWorkerRegistration")}}  interface creates a notification on an active service worker.

Syntax

showNotification(title)
showNotification(title, options)

Parameters

Return value

A {{jsxref('Promise')}}  that resolves to undefined.

Exceptions

Examples

navigator.serviceWorker.register("sw.js");

function showNotification() {
  Notification.requestPermission().then((result) => {
    if (result === "granted") {
      navigator.serviceWorker.ready.then((registration) => {
        registration.showNotification("Vibration Sample", {
          body: "Buzz! Buzz!",
          icon: "../images/touch/chrome-touch-icon-192x192.png",
          vibrate: [200, 100, 200, 100, 200, 100, 200],
          tag: "vibration-sample",
        });
      });
    }
  });
}

To invoke the above function at an appropriate time, you could listen to the {{domxref("ServiceWorkerGlobalScope.notificationclick_event", "notificationclick")}}  event.

You can also retrieve details of the {{domxref("Notification")}} s that have been fired from the current service worker using {{domxref("ServiceWorkerRegistration.getNotifications()")}} .

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

In this article

View on MDN