Notifications API is the browser interface that lets a web page show a system notification outside the page, after the user has granted permission for the site's origin. It is defined by the WHATWG Notifications Standard. The permission state is one of three strings: default, granted or denied, and only granted lets a notification appear.
The Notification class works with two static members. Notification.permission reads the current state, and Notification.requestPermission() asks the user and returns a Promise that resolves to the new state. The older callback form is deprecated. Creating new Notification("Title", { body: "Text" }) shows a notification when the state is granted. If it is not granted, the object fires an error event.
There are two kinds. A non-persistent notification is created with the constructor from a page. A persistent notification is created by calling showNotification on a service worker registration, survives the page closing, and is the only kind that supports action buttons. Many mobile browsers throw a TypeError for the constructor, so the service worker route is the portable choice.
function notificationState() {
if (!('Notification' in globalThis)) return 'unsupported';
return Notification.permission;
}
console.log(typeof Notification, notificationState());
Run in Node.js 22.22.0, which has no Notification class, this prints the unsupported branch. In a browser it prints function and then default, granted or denied.
undefined unsupported
Call Notification.requestPermission() from a click handler and await the result. Browsers ignore or block requests that do not follow a user gesture, and Firefox has required one since version 72. Chrome and Firefox also require a secure context, and they block requests from cross-origin iframes. Ask after the user shows interest, such as pressing an "Enable alerts" button, not on page load.
The usual reasons are that permission is not granted, the page is not on HTTPS, or the operating system has notifications turned off for the browser. Check Notification.permission first. If it reads denied, the user has to change the setting in the browser. Operating system settings can also hide notifications without any error in the page.