Notifications API
The Notifications API sends system-level notifications. Request permission first, then show notifications even when the page is in the background.
The Notifications API sends system-level notifications. Request permission first, then show notifications even when the page is in the background.
async function showNotification() {
const permission = await Notification.requestPermission();
if (permission === "granted") {
new Notification("New Message", {
body: "You have a message from Alice.",
icon: "/img/icon.png",
badge: "/img/badge.png"
});
}
}
document.getElementById("notifyBtn").addEventListener("click", showNotification);
Only request notification permission in response to a user gesture (click).