files/en-us/web/api/serviceworkerregistration/shownotification/index.md
{{APIRef("Web Notifications")}}{{SecureContext_Header}} {{AvailableInWorkers}}
The showNotification() method of the
{{domxref("ServiceWorkerRegistration")}} interface creates a notification on an active
service worker.
showNotification(title)
showNotification(title, options)
title
options {{optional_inline}}
actions {{optional_inline}} {{experimental_inline}}
: An array of actions to display in the notification, for which the default is an empty array. Each element in the array can be an object with the following members:
action
title
icon {{optional_inline}}
navigate {{optional_inline}} {{experimental_inline}}
Appropriate responses are built using event.action within the {{domxref("ServiceWorkerGlobalScope.notificationclick_event", "notificationclick")}} event.
badge {{optional_inline}} {{experimental_inline}}
body {{optional_inline}}
data {{optional_inline}} {{experimental_inline}}
null.dir {{optional_inline}}
auto, which just adopts the browser's language setting behavior, but you can override that behavior by setting values of ltr and rtl (although most browsers seem to ignore these settings.)icon {{optional_inline}}
image {{optional_inline}} {{experimental_inline}}
lang {{optional_inline}}
navigate {{optional_inline}} {{experimental_inline}}
renotify {{optional_inline}} {{experimental_inline}}
false, which means they won't be notified.
If true, then tag also must be set.requireInteraction {{optional_inline}} {{experimental_inline}}
false.silent {{optional_inline}}
null, means to respect device defaults.
If true, then vibrate must not be present.tag {{optional_inline}}
timestamp {{optional_inline}}
vibrate {{optional_inline}} {{experimental_inline}}
silent must not be true.A {{jsxref('Promise')}} that resolves to undefined.
activating or activated.silent option is true and the vibrate option is specified.renotify option is true but the tag option is empty.DataCloneError {{domxref("DOMException")}}
data option failed for some reason.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}}
{{Compat}}