Skip to content

Instantly share code, notes, and snippets.

@theking2
Last active June 5, 2024 21:13
Show Gist options
  • Save theking2/26b9a2c311b51dd51f99608daade24f8 to your computer and use it in GitHub Desktop.
Save theking2/26b9a2c311b51dd51f99608daade24f8 to your computer and use it in GitHub Desktop.
JS Notification
class Notifier
{
#notificationTitle
construct( title ) {
this.#notificationTitle = title;
this.#askNotificationPermission();
};
#askNotificationPermission() {
function handlePermission(permission) {
if (!('permission' in Notification)) {
Notification.permission = permission;
}
if (permission === 'denied' || permission === 'default') {
console.log('Notification permission was denied.');
} else {
console.log('Notification permission granted.');
}
}
if (!("Notification" in window)) {
console.log("This browser does not support notifications.");
} else {
Notification.requestPermission().then((permission) => {
handlePermission(permission);
});
}
};
showNotificationOrAlert(message) {
if (Notification.permission === 'granted') {
new Notification(this.#title, {
body: message
});
} else
// maybe do some fancy popup from a libary
alert(message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment