Created
September 19, 2020 00:14
-
-
Save raazon/0c4cbc1236c1c0626403995d021d1955 to your computer and use it in GitHub Desktop.
HTML5 Browser Push Notification
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
function askForApproval(title = '', content = '', icon = '', url = '') { | |
if(Notification.permission === "granted") { | |
createNotification( | |
title, | |
content, | |
icon, | |
url | |
); | |
} | |
else { | |
Notification.requestPermission(permission => { | |
if(permission === 'granted') { | |
createNotification( | |
title, | |
content, | |
icon, | |
url | |
); | |
} | |
}); | |
} | |
} | |
function createNotification(title, text, icon, url) { | |
const noti = new Notification(title, { | |
body: text, | |
icon | |
}); | |
noti.onclick = function () { | |
window.open(url); | |
}; | |
} | |
askForApproval('This is title', 'This is body text', 'https://via.placeholder.com/40x40', 'https://example.com'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment