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.
This function will send a notification to the user.
// 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",
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment