JSFiddle - React, Tailwind, and code Playground
HTML
<button onclick="notifyMe()">Notify me!</button>
JavaScript
document.addEventListener('DOMContentLoaded', function () {
if (Notification.permission !== "granted")
Notification.requestPermission();
});
window.notifyMe = function() {
if (!Notification) {
//alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('Notification');
notification.onclick = function () {
window.focus();
};
setTimeout(function() {
notification.close()
}, 2000);
// Result: Uncaught TypeError: Illegal invocation
// also tried.....
// setTimeout(notification.close(), 2000);
// Result: notification stays open forever
// setTimeout('notification.close', 2000);
// Result: ReferenceError: notification is not defined
}
}