๐ข JavaScript Notifications API: That Little Popup

Imagine youโre playing a game on your computer, and suddenly you see a little popup on the corner of your screen saying:
โโก Your friend just came online!โ
Or maybe youโre watching YouTube, and you see:
โ๐ New video from your favorite channel.โ
That little popup is called a Notification. It doesnโt matter if youโre browsing another website or looking at something else on your computer โ the notification still shows up.
The Notifications API in JavaScript allows websites to do this: send you small pop-up messages outside the web page.
๐ How Notifications Work
To use notifications in JavaScript, there are 3 main steps:
-
Ask for Permission
- Websites canโt just send you random notifications.
-
The browser asks:
โDo you want to allow notifications from this site?โ
- You can choose Allow or Block.
-
Create a Notification
- If the user says Allow, the website can now create a popup message.
-
Example:
new Notification("Hello! Thanks for allowing notifications.");
-
Add Options (title, body, image, icon, etc.)
-
Notifications can be customized with:
- Title โ the main heading.
- Body โ extra text.
- Icon โ a small picture (like a logo).
- Image โ a bigger picture (for rich notifications).
-
๐ผ Example โ Simple Notification
// First, ask for permission
Notification.requestPermission().then(function(permission) {
if (permission === "granted") {
new Notification("Welcome!", {
body: "Youโll now get updates from us ๐",
icon: "https://example.com/icon.png"
});
}
});
๐ What happens here:
- The browser asks if the user wants notifications.
- If yes, a pop-up shows with a title โWelcome!โ, text saying โYouโll now get updates from us ๐โ, and a small icon.
๐ฎ Real-Life Examples for a 14-Year-Old
- Gaming websites: โYour daily reward is ready!โ
- School apps: โYou have a new assignment to submit.โ
- Chat apps: โNew message from Alex.โ
- YouTube-like sites: โA new video is live now.โ
โก Important Rules
- Permission is a must โ The user decides.
- Donโt spam โ Too many notifications = Annoying!
- Notifications work even when the user is not on your page โ Thatโs their power.
โ Summary
- Notifications are like mini pop-ups from websites.
- They can appear even if youโre doing something else.
- JavaScript can create these using the Notifications API.
- First, ask the user politely. If they say yes โ you can send them cool reminders, updates, or alerts.
๐ Review โ Fill in the Blanks
- Notifications are small _______ that appear on your screen.
- To use notifications, websites must first ask for _______.
- The JavaScript object used to create a notification is called _______.
- A notification can have a title, body, icon, and _______.
- Too many notifications can _______ the user.
- If permission is _______ , a site cannot send notifications.
- Notifications are useful for alerts, updates, and _______ messages.
- The code
new Notification("Hi!")shows a notification with the text _______. - Notifications can appear even when the site is not _______.
- The method
Notification.requestPermission()is used to _______ permission.