0Pricing
React Academy · Lesson

Notifications API and Permission Handling

Request notification permissions at the right moment and send browser notifications from React.

Notifications API and Permission Handling is a free React Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Notification.permission Property

Notification.permission is a static string that reflects the current permission state. It can be "granted" (user allowed), "denied" (user blocked), or "default" (never asked). Check this property before trying to show notifications to avoid unnecessary errors.

Requesting Permission

Notification.requestPermission() returns a Promise that resolves with the new permission string. Call it only in response to a clear user action, not on page load. Browsers ignore or suppress permission prompts that fire immediately on load, and users find them annoying when they appear without context.

Best Practice: Request on User Action

The most effective pattern is to show an in-app prompt explaining the benefit, then let the user click "Enable notifications" before calling requestPermission(). This gives context, increases acceptance rates, and avoids the browser marking your site as spammy. Never request permission on page load.

Creating a Notification

new Notification(title, options) creates and displays a system notification. The first argument is the title string. The options object is the second argument and accepts properties like body (subtitle text), icon (image URL), and tag for deduplication. This only works if permission is "granted".

Notification Options

Key notification options include: body for descriptive text below the title, icon for a small image, badge for the small monochrome icon shown in the OS notification area, image for a large inline image, tag to replace a previous notification with the same tag, and requireInteraction: true to keep the notification until the user acts.

Notification Events

The Notification instance fires several events: onclick fires when the user clicks the notification (use it to focus the window and navigate), onclose fires when it is dismissed, and onerror fires if there was a problem creating it. Attach handlers to the returned instance after calling new Notification().

Programmatically Closing Notifications

Store the returned Notification instance in a variable and call notification.close() to dismiss it programmatically. This is useful for time-sensitive notifications that become irrelevant after a period. Combine with setTimeout to auto-close after a few seconds.

HTTPS Requirement for Notifications

The Notifications API requires a secure context. On HTTP pages, typeof Notification === 'undefined' or the API is disabled entirely depending on the browser. This is a security measure to prevent malicious sites from flooding users with fake system messages. Always check availability before using.

Building useNotifications Hook

A useNotifications hook can expose two methods: requestPermission() and showNotification(title, options). It internally tracks the current permission state and updates it after requesting. Components call showNotification without worrying about permission checks, since the hook handles that internally.

Combining with the Visibility API

The Page Visibility API exposes document.hidden and the visibilitychange event. Notifications make most sense when the user is not already viewing the tab. Check document.hidden === true before showing a notification to avoid redundant alerts when the page is already in the foreground.

Service Workers and Push Notifications

The Notifications API shown here is for locally triggered notifications from the page itself. For push notifications that arrive even when the page is closed, you need the Push API combined with a service worker. The service worker receives the push event and calls self.registration.showNotification() from the background.

Notification.permission Values

Which of the following is NOT a valid value for Notification.permission?

Lesson Recap: Notifications API

Notification.permission can be granted, denied, or default. Always request permission on a user action, not on page load. Use new Notification(title, options) to create notifications and handle onclick to focus the window. Combine with the Visibility API to show notifications only when the tab is hidden.

Frequently asked questions

Is the “Notifications API and Permission Handling” lesson free?

Yes — the full text of “Notifications API and Permission Handling” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “Notifications API and Permission Handling”?

Request notification permissions at the right moment and send browser notifications from React. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Notifications API and Permission Handling” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Clipboard API: Copy and Paste in React
  2. Geolocation API in React Components
  3. Notifications API and Permission Handling
  4. IntersectionObserver for Scroll-Triggered Effects
← Back to React Academy