📢 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.