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:

  1. 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.
  2. Create a Notification

    • If the user says Allow, the website can now create a popup message.
    • Example:

      new Notification("Hello! Thanks for allowing notifications.");
      
  3. 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

  1. Permission is a must โ†’ The user decides.
  2. Donโ€™t spam โ†’ Too many notifications = Annoying!
  3. 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

  1. Notifications are small _______ that appear on your screen.
  2. To use notifications, websites must first ask for _______.
  3. The JavaScript object used to create a notification is called _______.
  4. A notification can have a title, body, icon, and _______.
  5. Too many notifications can _______ the user.
  6. If permission is _______ , a site cannot send notifications.
  7. Notifications are useful for alerts, updates, and _______ messages.
  8. The code new Notification("Hi!") shows a notification with the text _______.
  9. Notifications can appear even when the site is not _______.
  10. The method Notification.requestPermission() is used to _______ permission.

<
Previous Post
JavaSript File API, Blobs, and FileReaders: A Comprehensive Explanation
>
Next Post
JavaScript Vibration API: How To Make The Device Buzz