W3cubDocs

/Web APIs

Notification

Note: This feature is available in Web Workers

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The Notification interface of the Notifications API is used to configure and display desktop notifications to the user.

These notifications' appearance and specific functionality vary across platforms but generally they provide a way to asynchronously provide information to the user.

EventTarget Notification

Constructor

Notification()

Creates a new instance of the Notification object.

Static properties

These properties are available only on the Notification object itself.

Notification.permission Read only

A string representing the current permission to display notifications. Possible values are:

  • denied — The user refuses to have notifications displayed.
  • granted — The user accepts having notifications displayed.
  • default — The user choice is unknown and therefore the browser will act as if the value were denied.
Notification.maxActions Read only Experimental

The maximum number of actions supported by the device and the User Agent.

Instance properties

These properties are available only on instances of the Notification object.

Notification.actions Read only Experimental

The actions array of the notification as specified in the constructor's options parameter.

Notification.badge Read only

The URL of the image used to represent the notification when there is not enough space to display the notification itself.

Notification.body Read only

The body string of the notification as specified in the constructor's options parameter.

Notification.data Read only

Returns a structured clone of the notification's data.

Notification.dir Read only

The text direction of the notification as specified in the constructor's options parameter.

Notification.lang Read only

The language code of the notification as specified in the constructor's options parameter.

Notification.tag Read only

The ID of the notification (if any) as specified in the constructor's options parameter.

Notification.icon Read only

The URL of the image used as an icon of the notification as specified in the constructor's options parameter.

Notification.image Read only Experimental

The URL of an image to be displayed as part of the notification, as specified in the constructor's options parameter.

Notification.renotify Read only Experimental

Specifies whether the user should be notified after a new notification replaces an old one.

Notification.requireInteraction Read only Experimental

A boolean value indicating that a notification should remain active until the user clicks or dismisses it, rather than closing automatically.

Notification.silent Read only

Specifies whether the notification should be silent — i.e., no sounds or vibrations should be issued, regardless of the device settings.

Notification.timestamp Read only Experimental

Specifies the time at which a notification is created or applicable (past, present, or future).

Notification.title Read only

The title of the notification as specified in the first parameter of the constructor.

Notification.vibrate Read only Experimental

Specifies a vibration pattern for devices with vibration hardware to emit.

Static methods

These methods are available only on the Notification object itself.

Notification.requestPermission()

Requests permission from the user to display notifications.

Instance methods

These properties are available only on an instance of the Notification object or through its prototype. The Notification object also inherits from the EventTarget interface.

Notification.close()

Programmatically closes a notification instance.

Events

click

Fires when the user clicks the notification.

close

Fires when the user closes the notification.

error

Fires when the notification encounters an error.

show

Fires when the notification is displayed.

Examples

Assume this basic HTML:

html

<button onclick="notifyMe()">Notify me!</button>

It's possible to send a notification as follows — here we present a fairly verbose and complete set of code you could use if you wanted to first check whether notifications are supported, then check if permission has been granted for the current origin to send notifications, then request permission if required, before then sending a notification.

js

function notifyMe() {
  if (!("Notification" in window)) {
    // Check if the browser supports notifications
    alert("This browser does not support desktop notification");
  } else if (Notification.permission === "granted") {
    // Check whether notification permissions have already been granted;
    // if so, create a notification
    const notification = new Notification("Hi there!");
    // …
  } else if (Notification.permission !== "denied") {
    // We need to ask the user for permission
    Notification.requestPermission().then((permission) => {
      // If the user accepts, let's create a notification
      if (permission === "granted") {
        const notification = new Notification("Hi there!");
        // …
      }
    });
  }

  // At last, if the user has denied notifications, and you
  // want to be respectful there is no need to bother them anymore.
}

We no longer show a live sample on this page, as Chrome and Firefox no longer allow notification permissions to be requested from cross-origin <iframe>s, with other browsers to follow. To see an example in action, check out our To-do list example (also see the app running live).

Note: In the above example we spawn notifications in response to a user gesture (clicking a button). This is not only best practice — you should not be spamming users with notifications they didn't agree to — but going forward browsers will explicitly disallow notifications not triggered in response to a user gesture. Firefox is already doing this from version 72, for example.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
Notification 20 14 224 No 23 7 No No 224 No 16.4 No
Notification
20["Starting in Chrome 49, notifications do not work in incognito mode.", "Before Chrome 42, service worker additions were not supported."]
14 224 No
23["Starting in Opera 36, notifications do not work in incognito mode.", "Before Opera 29, service worker additions were not supported."]
7 No
42["Notifications in Chrome for Android are only available through service workers. To show notifications on Android, see ServiceWorkerRegistration.showNotification().", "Starting in Chrome 49, notifications do not work in incognito mode."]
224
29["Notifications in Opera for Android are only available through service workers. To show notifications on Android, see ServiceWorkerRegistration.showNotification().", "Starting in Opera for Android 36, notifications do not work in incognito mode."]
16.4
4.0["Notifications in Samsung Internet are only available through service workers. To show notifications on Android, see ServiceWorkerRegistration.showNotification().", "Starting in Samsung Internet 5.0, notifications do not work in incognito mode."]
actions 53 18 No No 39 No No 53 No 41 No 6.0
badge 53 18 No No 39
17Badging is supported for web apps saved to the Dock in Safari 17 on the macOS Sonoma beta
No 53 No 41 16.4 6.0
body 33 14 26 No 23 11 No 42 26 29 16.4 4.0
click_event 20 14 22 No 23 7 No 42 22 29 No 4.0
close 20 14 22 No 23 7 No 42 22 29 16.4 4.0
close_event 20 14 22 No 23 7 No 42 22 29 No 4.0
data 44 16 34 No 31 16 No 44 34 32 16.4 4.0
dir 20 14 26 No 23 7 No 42 26 29 16.4 4.0
error_event 20 14 22 No 23 7 No 42 22 29 No 4.0
icon 33 14 26 No 23 11 No 42 26 29 16.4 4.0
image 56 18 No No 43 No No 56 No 43 No 6.0
lang 33 14 26 No 23 11 No 42 26 29 16.4 4.0
maxActions_static 48 18 No No 35 No No 48 No 35 No 5.0
permission_static 32 14 22 No 23 7 No 42 22 29 16.4 4.0
renotify 50 79 No No 37 No No 50 No 37 No 5.0
requestPermission_static 20 14
22["From Firefox 70 onwards, cannot be called from a cross-origin iframe.", "From Firefox 72 onwards, can only be called in response to a user gesture such as a click event."]
No 23 15
7–15Only supported the deprecated callback syntax.
No 42
22["From Firefox Android 79 onwards, cannot be called from a cross-origin iframe.", "From Firefox Android 79 onwards, can only be called in response to a user gesture such as a click event."]
29 16.4 4.0
requireInteraction 47 17
117Only supported on Windows. Behind a flag on other operating systems.
117
No 34 No No 47
117Only supported on Windows. Behind a flag on other operating systems.
34 No 5.0
secure_context_required 62 79 67 No 49 No No 62 67 46 No 8.0
show_event 20 14 22 No 23 7 No 42 22 29 No 4.0
silent 43 17 No No 30 16.6 No 43 No 30 16.6 4.0
tag 20 14 26 No 23 7 No 42 26 29
NoThis property is exposed but is not implemented. See WebKit bug 258922.
4.0
timestamp 50 17 No No 37 No No 50 No 37 No 5.0
title 33 14 26 No 23 11 No 42 26 29 16.4 4.0
vibrate 53 79 No No 40 No No
53Does not work on Android O or later regardless of Chrome version.
No
41Does not work on Android O or later regardless of Chrome version.
No
6.0Does not work on Android O or later regardless of Chrome version.
worker_support 42 15 41 No 29 No No 42 41 29 No 4.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Notification