Last active
October 10, 2022 12:32
-
-
Save hwclass/89540f7bb0379d3c37d625a43584a688 to your computer and use it in GitHub Desktop.
Code piece for easily self-notification implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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