W3cubDocs

/Web APIs

Notification: requestPermission() static method

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

The requestPermission() static method of the Notification interface requests permission from the user for the current origin to display notifications.

Syntax

js

// The latest spec has updated this method to a promise-based syntax that works like this:
Notification.requestPermission()

// Previously, the syntax was based on a simple callback; this version is now deprecated:
Notification.requestPermission(callback)

Parameters

callback Optional Deprecated

An optional callback function that is called with the permission value. Deprecated in favor of the promise return value.

Return value

A Promise that resolves to a string with the permission picked by the user. Possible values for this string are:

  • granted
  • denied

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
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

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/requestPermission_static