docs.rodeo

MDN Web Docs mirror

Notification

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

The Notification interface of the {{domxref("Notifications API", "", "", "nocode")}}  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.

{{InheritanceDiagram}} 

Constructor

Static properties

Also inherits properties from its parent interface, {{domxref("EventTarget")}} .

Instance properties

Also inherits properties from its parent interface, {{domxref("EventTarget")}} .

Static methods

Also inherits methods from its parent interface, {{domxref("EventTarget")}} .

Instance methods

Also inherits methods from its parent interface, {{domxref("EventTarget")}} .

Events

Also inherits events from its parent interface, {{domxref("EventTarget")}} .

Examples

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() {
  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 {{htmlelement("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

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN