JavaScript Notifications API: Complete Tutorial

Learn how to display desktop notifications from JavaScript using the Notifications API, request permission, and handle notification clicks.

5 min read

The Notifications API lets your web app display system-level desktop notifications that appear outside the browser window. Unlike alert() which blocks the page and only shows inside the tab, notifications appear in the operating system's notification center and remain visible even when the browser is minimized.

Here is the smallest working notification you can send:

javascriptjavascript
const notification = new Notification("New message received");

This creates a notification with the title "New message received". It appears briefly in the corner of the screen, then disappears. The user can see it even when they are in a different application.

Checking and requesting permission

Before sending notifications, you must check the current permission state. Browsers deny notifications by default, and the user must explicitly grant permission.

The permission state lives on Notification.permission and has three possible values. The string "granted" means the user allowed notifications and you can send them freely. The string "denied" means the user blocked them and you cannot ask again. The string "default" means the user has not decided yet and you need to request permission.

javascriptjavascript
if (Notification.permission === "granted") {
  new Notification("You have already granted permission");
} else if (Notification.permission !== "denied") {
  Notification.requestPermission().then((permission) => {
    if (permission === "granted") {
      new Notification("Thanks for allowing notifications");
    }
  });
}

The requestPermission method must be called from a user gesture like a button click. Calling it on page load is blocked by modern browsers. For a complete walkthrough of the permission flow, see the companion article on notification permissions.

Adding body, icon, and tag

The Notification constructor accepts an options object as its second argument. These properties customize how the notification looks and behaves:

javascriptjavascript
const notification = new Notification("Build complete", {
  body: "The project compiled successfully with no errors.",
  icon: "/icons/build-success.png",
  tag: "build-status",
});

The body property adds descriptive text below the title. The icon property sets a small image shown next to the title. The tag property groups related notifications, so sending a second notification with the same tag replaces the first one instead of stacking.

Handling notification clicks

When the user clicks a notification, you can respond by listening for the click event. A common pattern is to focus the browser window or navigate to a specific page:

javascriptjavascript
const notification = new Notification("New comment", {
  body: "Rune replied to your post",
  tag: "comment-reply",
});
 
notification.addEventListener("click", () => {
  window.focus();
  console.log("User clicked the notification");
});

The click handler fires when the user interacts with the notification. Calling window.focus brings the browser tab to the foreground, which is what most users expect after clicking a notification.

Closing notifications programmatically

Notifications auto-dismiss after a few seconds on most platforms, but you can also close them from code:

javascriptjavascript
const notification = new Notification("Processing");
setTimeout(() => {
  notification.close();
}, 3000);

The close method removes the notification from the screen. Combined with the close event, you can track when a notification disappears whether by user action or timeout.

Common mistakes

  • Calling requestPermission on page load instead of on a user gesture. Modern browsers silently ignore it.
  • Not checking Notification.permission before creating a notification. Calling the constructor with "denied" permission does nothing or throws.
  • Forgetting that notifications need a secure context. The API requires HTTPS or localhost.
  • Expecting notification actions and images to work consistently across all browsers and operating systems.
  • Using notifications for critical information. Users may have notifications disabled system-wide.

Quick reference

OperationCode
Check permissionNotification.permission
Request permissionNotification.requestPermission()
Send notificationnew Notification(title, { body, icon, tag })
Handle clicknotification.addEventListener("click", handler)
Close notificationnotification.close()
Requires secure contextYes (HTTPS or localhost)
Rune AI

Rune AI

Key Insights

  • Use new Notification(title, options) to display a desktop notification.
  • Check Notification.permission before creating a notification.
  • Request permission only on a user gesture like a button click.
  • Notifications appear at the OS level and work even with the tab in the background.
  • Handle the click event to focus the app window when the user interacts with a notification.
RunePowered by Rune AI

Frequently Asked Questions

Do notifications work when the browser tab is in the background?

Yes. Notifications appear at the operating system level, so they are visible even when the browser is minimized or the tab is inactive. This is their main advantage over in-page alerts.

Why do my notifications not appear on mobile?

The Notifications API has limited support on mobile browsers. Android Chrome supports it, but iOS Safari has historically lagged behind. Always check Notification.permission and test on target devices.

Can I include images or action buttons in notifications?

You can always add an icon URL for a small image. Action buttons need the actions array, but that option is only honored on notifications shown through a service worker's showNotification method, not on notifications created directly with the Notification constructor.

How do notifications differ from alert()?

alert() blocks the page and only shows in the browser tab. Notifications appear at the OS level, do not block JavaScript execution, and can include richer content like icons and body text.

Conclusion

The Notifications API lets your web app reach users outside the browser tab. Check permission first, request it on a user gesture, and construct notifications with a title and optional body, icon, and tag. Always handle the case where the user denies permission: the API is a privilege, not a right.