Notification: requestPermission() static method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

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.

The method returns a Promise that fulfills with a string indicating whether permission was granted or denied.

Syntax

js
Notification.requestPermission()

// Deprecated syntax using a callback
Notification.requestPermission(callback)

Parameters

callback Optional

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

The user has explicitly granted permission for the current origin to display system notifications.

denied

The user has explicitly denied permission for the current origin to display system notifications.

default

The user decision is unknown; in this case the application will act as if permission was denied.

The deprecated version of the method returns undefined.

Examples

Assume this basic HTML:

html
<button>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.

Note that the request should be made in response to user interaction: below, the method is called in the click event handler.

js
document.querySelector("button").addEventListener("click", notifyMe);

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

Specifications

Specification
Notifications API
# dom-notification-requestpermission

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
requestPermission() static method
Chrome – Full support
Chrome 20 (Release date: 2012-06-26)
footnote Full support
Edge – Full support
Edge 14 (Release date: 2016-08-02)
footnote Full support
Firefox – Full support
Firefox 22 (Release date: 2013-06-25)
footnote
footnote From Firefox 70 onwards, cannot be called from a cross-origin iframe.
footnote From Firefox 72 onwards, can only be called in response to a user gesture such as a click event.
Opera – Full support
Opera 23 (Release date: 2014-07-22)
footnote Full support
Safari – Partial support
Safari 7 – 14.1 (Release date: 2013-10-22)
footnote Partial support
footnote Only supported the deprecated callback syntax.
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 42 (Release date: 2015-04-15)
footnote Full support
Firefox for Android – Full support
Firefox for Android 22 (Release date: 2013-06-25)
footnote
footnote From Firefox Android 79 onwards, cannot be called from a cross-origin iframe.
footnote From Firefox Android 79 onwards, can only be called in response to a user gesture such as a click event.
Opera Android – Full support
Opera Android 29 (Release date: 2015-04-28)
footnote Full support
Safari on iOS – Partial support
Safari on iOS 16.4 (Release date: 2023-03-27)
footnote Partial support
footnote The parent Notification interface is undefined unless the page is a web app saved to the home screen. The app's manifest must have a non-default display value.
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – No support
WebView Android
footnote
footnote See bug 40443309
WebView on iOS – No support
WebView on iOS
footnote No support
Returns a Promise
Chrome – Full support
Chrome 47 (Release date: 2015-12-01)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 46 (Release date: 2016-04-26)
footnote Full support
Opera – Full support
Opera 34 (Release date: 2015-12-08)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 47 (Release date: 2015-12-02)
footnote Full support
Firefox for Android – Full support
Firefox for Android 46 (Release date: 2016-04-26)
footnote Full support
Opera Android – Full support
Opera Android 34 (Release date: 2015-12-16)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16.4 (Release date: 2023-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 5 (Release date: 2016-12-15)
footnote Full support
WebView Android – No support
WebView Android
footnote
footnote See bug 40443309
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
See implementation notes.
Has more compatibility info.

See also