Course contentsShow
← Web Development
Lesson 2345 of 2,98242. Progressive Web AppsFree lesson

Requesting Notification Permission

Using Notification.requestPermission() and handling permission states (granted, denied, default).

Requesting Notification Permission

What you'll learn: How to ask users for permission to display push notifications and handle their response.

Before your Progressive Web App can show notifications, you must obtain explicit permission from the user. This is a browser-enforced security measure that respects user privacy and prevents notification spam.

The Permission Request

The Notification.requestPermission() method prompts the browser to display a permission dialog. This method returns a Promise that resolves with the user's decision:

Notification.requestPermission().then(permission => {
  console.log('Permission result:', permission);
});

The Three Permission States

The user's response will be one of three values:

  • "granted" — User accepted; you can now show notifications
  • "denied" — User declined; you cannot show notifications (and shouldn't ask again)
  • "default" — User dismissed the prompt without choosing; you may ask again later

Checking Current Permission

Before requesting permission, check if you already have it:

if (Notification.permission === 'granted') {
  console.log('Already have permission');
} else if (Notification.permission === 'denied') {
  console.log('Permission denied, cannot request again');
} else {
  // Permission is "default" - safe to request
  Notification.requestPermission().then(permission => {
    if (permission === 'granted') {
      console.log('Permission granted!');
    }
  });
}

Best Practices

Always request permission in response to a user action (like clicking a "Enable Notifications" button), not automatically on page load. This contextualizes the request and improves acceptance rates. Think of it like asking to borrow something—timing and context matter.

Key Takeaway: Use Notification.requestPermission() to ask for notification permission, then handle the three possible states (granted, denied, default) to provide appropriate user experiences and respect their choices.