--- title: ServiceWorkerRegistration.showNotification() slug: Web/API/ServiceWorkerRegistration/showNotification translation_of: Web/API/ServiceWorkerRegistration/showNotification ---

{{SeeCompatTable}}{{APIRef("Service Workers API")}}

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

Note: This feature is available in Web Workers.

Syntax

​ServiceWorkerRegistration.showNotification(title, [options]).then(function(NotificationEvent) { ... });

Returns

A {{jsxref('Promise')}} that resolves to a {{domxref('NotificationEvent')}}.

Parameters

title
The title that must be shown within the notification
options {{optional_inline}}
An object that allows to configure the notification. It can have the following properties:

Examples

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

function showNotification() {
  Notification.requestPermission(function(result) {
    if (result === 'granted') {
      navigator.serviceWorker.ready.then(function(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 use the {{domxref("ServiceWorkerGlobalScope.onnotificationclick")}} event handler.

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

Specifications

Specification Status Comment
{{SpecName('Web Notifications','#dom-serviceworkerregistration-shownotificationtitle-options','showNotification()')}} {{Spec2('Web Notifications')}} Initial definition.

Browser compatibility

{{Compat}}

{{Compat("api.ServiceWorkerRegistration.showNotification")}}