ReviseAlgo Logo

Browser APIs & Web APIs

Notifications API

Master system notifications in JavaScript. Learn requesting user permission, creating Notification instances, and lifecycle event handlers.

Last Updated: July 15, 2026 10 min read

1. Introduction

The Notifications API allows web applications to display system notifications to the user. These notifications are displayed outside the browser tab, usually matching the operating system's native notification style, even if the browser is running in the background.

2. Why It Matters

Displaying notifications is key to building real-time collaboration tools, like chat applications or calendar reminders. Knowing how to request permissions and handle notification clicks ensures users stay updated and engaged.

3. Real-World Analogy

Think of a Paging system at a Hospital:

  • Permission check: The clinic desk receptionist asks: "Do we have permission to call your phone when the doctor is ready?" (Permission prompt). You select: Granted, Denied, or Default (unset).
  • Granted state: If you select "Granted", the desk pager flashes and plays a sound (the notification) when your turn arrives. Even if you are sitting in the courtyard outside (browser is in the background), you receive the update.

4. The Notifications API

Using the API involves requesting permission and then creating a Notification instance:

1. Requesting Permission:

Call the static method Notification.requestPermission(). This returns a Promise that resolves to the permission state (granted, denied, or default).

2. Creating a Notification:

Create a new Notification instance, passing the title and an optional configuration options object (containing body text, icons, or actions).

5. Practical Example

This script demonstrates wrapping the permission and creation logic into a single helper function:

6. Common Mistakes

  • Requesting permission on page load: Bombarding users with a permission prompt immediately when they load the page often leads to them clicking "Block". Instead, request permission after a user action (like clicking a "Notify Me" button) to explain why the permission is needed.
  • Assuming Notification is supported on mobile browsers: Many mobile browsers (like iOS Safari) do not support the standard new Notification() constructor in the main thread. To send notifications on mobile, use the Web Push API combined with Service Worker registrations instead.

7. Quick Quiz

Q1: Which permission state indicates that the user has blocked the application from displaying notifications?

A) default

B) denied

Answer: B — The "denied" permission state indicates the user has blocked notifications. The browser will ignore any subsequent requests to show notifications.

8. Scenario-Based Challenge

The Spam Flooding Guard:

An application receives chat updates frequently. If 5 messages arrive in 2 seconds, displaying 5 separate notification popups will annoy the user. Write a notification wrapper that uses the tag option parameter to overwrite previous notifications, ensuring only the latest message is visible.

9. Debugging Exercise

Explain why this notification script crashes on iOS Safari, and how to fix it:

// Objective: alert task completion
const alert = new Notification('Timer Ended', { body: 'Finished!' }); // Crashes on iOS! Why?
View Solution

Diagnosis: The standard new Notification() constructor is not supported in the main execution thread on iOS Safari. Running this throws a TypeError: undefined is not an object.

Fix: Wrap your notification calls inside a check to verify support before creating the notification, and fall back to using Service Worker registrations (e.g. registration.showNotification()) if needed:

if ('Notification' in window) {
  if (Notification.permission === 'granted') {
    new Notification('Timer Ended', { body: 'Finished!' });
  }
} else {
  // Geolocation / fallback sound or custom modal toast alert
  alert('Timer Ended: Finished!');
}

10. Interview Questions

🟢 Q1: Describe how to display desktop notifications in modern browsers and check permission state.

Answer:
1. Check permission: Inspect the Notification.permission property (values: granted, denied, or default).
2. Request permission: Call Notification.requestPermission() if permission has not been granted yet.
3. Create notification: If permission is granted, create a new Notification(title, options) instance. The browser will then display the notification.

11. Production Considerations

  • User Experience (UX): Avoid showing unsolicited notifications. Let users customize which events trigger notifications in their settings panel to prevent them from blocking notifications entirely.