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

Note: This feature is available in Web Workers.

Constructor

Notification()
Creates a new instance of the Notification object.

Properties

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.

Instance properties

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

Notification.actions Read only
The actions array of the notification as specified in the options parameter of the constructor.
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 options parameter of the constructor.
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 options parameter of the constructor.
Notification.lang Read only
The language code of the notification as specified in the options parameter of the constructor.
Notification.tag Read only
The ID of the notification (if any) as specified in the options parameter of the constructor.
Notification.icon Read only
The URL of the image used as an icon of the notification as specified in the options parameter of the constructor.
Notification.image Read only
The  URL of an image to be displayed as part of the notification, as specified in the options parameter of the constructor.
Notification.renotify Read only
Specifies whether the user should be notified after a new notification replaces an old one.
Notification.requireInteraction Read only
A Boolean 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
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
Specifies a vibration pattern for devices with vibration hardware to emit.

Unsupported properties

The following properties are listed in the most up-to-date spec but are not supported in any browsers yet. It is advisable to keep checking back regularly to see if the status of these has updated, and let us know if you find any out of date information.

Notification.noscreen Read only
Specifies whether the notification firing should enable the device's screen or not.
Notification.sticky Read only
Specifies whether the notification should be 'sticky', i.e. not easily clearable by the user.

Event handlers

Notification.onclick
A handler for the click event. It is triggered each time the user clicks on the notification.
Notification.onclose
A handler for the close event. It is triggered when the user closes the notification.
Notification.onerror
A handler for the error event. It is triggered each time the notification encounters an error.
Notification.onshow
A handler for the show event. It is triggered when the notification is displayed.

Methods

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.

Example

Assume this basic 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.

function notifyMe() {
  // Let's check if the browser supports notifications
  if (!("Notification" in window)) {
    alert("This browser does not support desktop notification");
  }

  // Let's check whether notification permissions have already been granted
  else if (Notification.permission === "granted") {
    // If it's okay let's create a notification
    var notification = new Notification("Hi there!");
  }

  // Otherwise, we need to ask the user for permission
  else if (Notification.permission !== "denied") {
    Notification.requestPermission(function (permission) {
      // If the user accepts, let's create a notification
      if (permission === "granted") {
        var 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 any more.
}

Alternate example: run on page load

In many cases, you don't need to be this verbose. For example, in our Emogotchi demo (see source code), we simply run Notification.requestPermission regardless to make sure we can get permission to send notifications (this uses the newer promise-based method syntax):

Notification.requestPermission().then(function(result) {
  console.log(result);
});

Then we run a simple spawnNotification() function when we want to fire a notification — this is passed arguments to specify the body, icon and title we want, then it creates the necessary options object and fires the notification using the Notification() constructor.

function spawnNotification(body, icon, title) {
  var options = {
      body: body,
      icon: icon
  };
  var n = new Notification(title, options);
}

Specifications

Specification Status Comment
Notifications API Living Standard Living standard

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support

221

5 -webkit-

Yes

22

4 -moz-

No256
Available in workers45 Yes41 No32 ?
Secure contexts only62 ? ? No49 ?
actions53 No No No39 ?
badge53 No No No39 ?
body Yes ? Yes No ? ?
data Yes ? Yes No ? ?
dir Yes ? Yes No ? ?
icon

22

5 -webkit-

?

22

4 -moz-

No25 No
image53 ? No No40 ?
lang Yes ? Yes No ? ?
maxActions Yes ? No No ? ?
onclick Yes ? No No ? ?
onclose Yes ? Yes No ? ?
onerror Yes ? No No ? ?
onshow Yes ? Yes No ? ?
permission Yes ? Yes No ? ?
renotify50 No No No37 No
requireInteraction Yes17 No No ? ?
silent4317 No No30 No
tag Yes ? Yes No ? ?
timestamp Yes17 No No ? ?
title Yes ? No No ? ?
vibrate53 No No No39 ?
close Yes ? Yes No ? ?
requestPermission46 ?47 No40 ?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support No Yes ?

22

4 -webkit-

Yes No ?
Available in workers No45 Yes4132 No ?
Secure contexts only No62 ? ?49 No ?
actions No53 No No39 No ?
badge No53 No No39 No ?
body No Yes ? Yes ? No ?
data No Yes ? Yes ? No ?
dir No Yes ? Yes ? No ?
icon No Yes ?

22

4 -moz-

Yes No ?
image No53 ? No40 No ?
lang No Yes ? Yes ? No ?
maxActions No Yes ? No ? No ?
onclick No Yes ? No ? No ?
onclose No Yes ? Yes ? No ?
onerror No Yes ? No ? No ?
onshow No Yes ? Yes ? No ?
permission No Yes ? Yes ? No ?
renotify No50 No No37 No ?
requireInteraction No Yes17 No ? No ?
silent No4317 No30 No ?
tag No Yes ? Yes ? No ?
timestamp No Yes17 No ? No ?
title No Yes ? No ? No ?
vibrate No53 No No39 No ?
close No Yes ? Yes ? No ?
requestPermission No46 ? Yes40 No ?

1. Before Chrome 22, the support for notification followed an old prefixed version of the specification and used the navigator.webkitNotifications object to instantiate a new notification. Before Chrome 32, Notification.permission was not supported. Before Chrome 42, service worker additions were not supported. Starting in Chrome 49, notifications do not work in incognito mode.

See also