Notifications API

On this page 7

The Notifications API is a browser interface that lets a web page or web application display system-level notifications outside the browser viewport, in the notification area of the operating system. Among Web APIs, it is the presentation layer for alerts: it controls how a message looks and how the user can interact with it, while other APIs decide when that message arrives.

Using the interface always requires explicit permission from the user, granted per origin. Notifications can be created from a visible page with the Notification constructor or from a service worker with showNotification(), and the second route is what allows messages to appear when no tab is open, in combination with web push.

How the Notifications API works

The flow has three stages: permission, creation and interaction. The page first checks Notification.permission, which is default, granted or denied. If the value is default, the page calls Notification.requestPermission(), and modern browsers accept that call only in response to a user gesture such as a click.

Once permission is granted, a script creates a notification with a title and an options object. The browser hands it to the operating system, which renders it in its own style, so the exact appearance differs between Windows, macOS, Android and Linux desktops.

OptionPurposePractical note
bodyMain message textKeep it short; systems truncate long text
iconImage identifying the senderSquare image, often 192 px
tagGroups or replaces related notificationsSame tag replaces the older message
requireInteractionKeeps the notification until acted onIgnored on some platforms
actionsUp to a small number of buttonsService worker notifications only
silentSuppresses sound and vibrationUseful for low-priority updates
dataArbitrary payload for click handlingStores a target address or identifier
Main notification options

Page notifications versus service worker notifications

The API offers two ways to show a notification, and the difference determines what an application can do. A page-level notification lives only as long as the page; a service worker notification is owned by the browser and survives closed tabs.

AspectNotification constructorServiceWorkerRegistration.showNotification
Requires an open pageYesNo
Action buttonsNot supportedSupported
Click handlingonclick on the objectnotificationclick event in the worker
Mobile supportLimited, often unavailableStandard route on Android
Typical useAlerts while the app is openPush messages and background events
The two creation paths compared

Because mobile browsers increasingly disallow the constructor, robust applications register a service worker and use showNotification() for every notification, even when the page is visible.

Relationship to web push and other APIs

The Notifications API is often confused with web push, but the two solve different problems. The Push API delivers a message from a server to a service worker; the Notifications API shows something to the user. A push message must normally lead to a visible notification, because browsers penalize or block silent pushes.

InterfaceWhat it doesRelation to notifications
Push APIReceives server messages in a service workerUsually triggers showNotification
Server-sent eventsStreams updates to an open pageCan trigger page-level notifications
WebSocketTwo-way real-time channelSame, but only while connected
Badging APISets a count on the app iconQuieter alternative to a notification
Permissions APIQueries permission stateReads notification permission without prompting
Notifications API and neighboring interfaces

Permission strategy and user experience

Permission requests are one-shot and hard to undo. When a user selects block, the page cannot ask again, and only the user can reverse the choice in browser settings. Browsers also apply quieter permission prompts to sites whose requests are widely rejected.

  • Ask in context: request permission after a user action that clearly benefits from alerts, such as following an order.
  • Explain first: show an in-page message describing what will be sent before triggering the browser prompt.
  • Offer control: provide in-app settings to choose notification types and frequency.
  • Respect focus: avoid notifying about events the user is already looking at on screen.

A prompt on the first page view without context is the most common reason for low acceptance. A delayed, explained prompt tends to produce fewer but more meaningful subscriptions.

Frequency matters as much as the initial request. Users who receive too many low-value alerts tend to revoke permission at the browser level, which removes the channel for every future message. A practical rule is to send notifications only for events that are personal, timely and actionable, such as a delivery status, a reply to the user or a security warning, and to group minor updates into a single summary message.

Constraints and common mistakes

Support varies across platforms. On iOS and iPadOS, web notifications work only for web apps added to the home screen, and focus modes or system settings can silence them entirely. The API also requires a secure context, meaning HTTPS or localhost.

ProblemCauseFix
Prompt never appearsRequest not tied to a user gestureCall requestPermission inside a click handler
Constructor throws on AndroidPage-level notifications unsupportedUse a service worker
Duplicate alertsSame event shown by several tabsUse a shared tag
Click opens a blank tabNo target stored with the notificationPut the address in the data option
Nothing shown on iPhoneSite not installed as a web appGuide users to add it to the home screen
Typical problems and fixes

In plain words

When a chat app on your phone shows a message on your lock screen, that is a notification. The Notifications API lets websites do the same thing, so a web app can tap you on the shoulder even when you are looking at another window.

A website can only do this after you say yes. If you refuse, it cannot keep asking, and you can switch notifications off again in your browser settings at any time.

A real-world example

A regional food delivery service runs its ordering platform as a web application. Customers previously kept the order page open and refreshed it to see whether the courier had left the restaurant.

The developers add a service worker and, after the customer places an order, show a short in-page message offering status updates, followed by the browser permission prompt. Courier events are sent through web push, and the service worker displays each one with showNotification(), using one shared tag per order so that each new status replaces the previous one.

Customers can now close the tab and still see pickup and arrival alerts in their system notification area. Because the prompt appears only at a meaningful moment, far fewer users block notifications than with the earlier prompt on the home page.