Skip to content

Instantly share code, notes, and snippets.

@mohitxskull
Created January 8, 2023 10:01
Show Gist options
  • Save mohitxskull/6ce64130a6cfb767d38b9d08dd49106d to your computer and use it in GitHub Desktop.
Save mohitxskull/6ce64130a6cfb767d38b9d08dd49106d to your computer and use it in GitHub Desktop.

Revisions

  1. mohitxskull created this gist Jan 8, 2023.
    29 changes: 29 additions & 0 deletions Notification.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    // This function will send a notification to the user.
    // If the user has not granted permission to show notifications
    // it will ask for permission.
    function sendNotification() {
    // Check if the browser supports notifications
    if (!("Notification" in window)) {
    console.log("This browser does not support notifications.");
    }
    // Check if the user has granted permission to show notifications
    else if (Notification.permission === "granted") {
    // If permission is granted, create a notification
    var notification = new Notification("Notification Title", {
    body: "This is the notification body.",
    icon: "https://example.com/icon.png",
    });
    }
    // If the user has not granted permission, ask for permission
    else if (Notification.permission !== "denied") {
    Notification.requestPermission().then(function (permission) {
    // If the user grants permission, create a notification
    if (permission === "granted") {
    var notification = new Notification("Notification Title", {
    body: "This is the notification body.",
    icon: "https://example.com/icon.png",
    });
    }
    });
    }
    }