Notifications API in JavaScript
Request a desktop reminder in JavaScript with Notification.requestPermission and new Notification. Respect denial; system settings can still hide the message.
Save this as index.html and serve it on HTTPS or localhost. Localhost is a web server on your own computer. Use the page buttons to request access.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<button id="notify">Remind me</button><p id="status"></p>7<script>8const status = document.querySelector('#status');9document.querySelector('#notify').onclick = async () => {10 if (!('Notification' in window)) { status.textContent = 'Notifications not supported.'; return; }11 try {12 const permission = Notification.permission === 'default'13 ? await Notification.requestPermission() : Notification.permission;14 if (permission !== 'granted') { status.textContent = 'Notifications not allowed.'; return; }15 new Notification('Library reminder', { body: 'Return your borrowed book today.' });16 status.textContent = 'Notification requested. Check your system settings if it is hidden.';17 } catch (error) { status.textContent = 'This browser cannot show this desktop notification.'; }18};19</script>20</body>21</html>
Sample Output
Library reminder
The requested desktop notification title. The system may hide it in quiet mode.
Return your borrowed book today.
The notification body, not a message printed in the console.
Notifications not allowed.
The permission check stopped notification creation.
In this one
- Check notification support
- Request permission after a click
- Distinguish default, granted, and denied
- Handle desktop-only limits
How it works
- 1
Wait for the Remind me button instead of prompting on page load.
- 2
Check that Notification exists.
- 3
Ask only when permission is default, then inspect the answer.
- 4
Create the desktop notification only when permission is granted.
- 5
Read the page status separately from the operating-system popup.
Edge cases
Permission already denied
The browser may not show another prompt.
Show the not-allowed message. The person can change site permissions themselves; code cannot override the refusal.
Mobile or quiet mode
The constructor may fail or the system may hide the popup.
The catch block explains failure. Do not claim delivery just because new Notification returned without an error.
What's going on
The Notifications API lets a page ask the operating system to show a message outside the page. An API is a browser feature that JavaScript can call. A library reminder is a useful example because the message is short and the person chooses to receive it.
This browser-only example is for supported desktop browsers on HTTPS or localhost. Clicking the button asks for permission only when the choice is still undecided. A denied choice is respected. A granted choice lets the code request a notification with a title and a body.
A successful request does not promise that a popup will be visible. Quiet mode or system settings can hide it. Many mobile browsers do not support this constructor; mobile sites normally use a service worker and showNotification instead. A service worker is a separate browser script that can handle certain tasks outside an open page.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which permission allows creation?
Pick an answer to check it.
Question 2
Where does the body appear?
Pick an answer to check it.
Questions people ask
Can I send this after the browser is closed?
This page example does not do that. Persistent notifications use a service worker and need a separate setup.
Why ask inside a button click?
Browsers restrict unsolicited permission prompts. A click also gives the person a clear choice.
Does granted mean the person read it?
No. It means notification permission is allowed, not that a message was displayed or read.