Skip to content

Instantly share code, notes, and snippets.

@hwclass
Last active October 10, 2022 12:32
Show Gist options
  • Save hwclass/89540f7bb0379d3c37d625a43584a688 to your computer and use it in GitHub Desktop.
Save hwclass/89540f7bb0379d3c37d625a43584a688 to your computer and use it in GitHub Desktop.
Code piece for easily self-notification implementation
// the function which enables us to send notification to the user in every 30 seconds
function randomNotification() {
const notifTitle = 'Title here';
const notifBody = `Body here...`;
const notifImg = 'http://image-url-here/static/image.png';
const options = {
body: notifBody,
icon: notifImg,
};
new Notification(notifTitle, options);
setTimeout(randomNotification, 30000);
}
// assume we have a button
const btn = document.querySelector("button");
// check if button exists, and attach `onclick` event
if (btn) {
btn.onclick = function() {
// get the permission for the notification
Notification.requestPermission().then((result) => {
if (result === 'granted') {
randomNotification();
}
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment