Skip to content

Instantly share code, notes, and snippets.

@raazon
Created September 19, 2020 00:14
Show Gist options
  • Save raazon/0c4cbc1236c1c0626403995d021d1955 to your computer and use it in GitHub Desktop.
Save raazon/0c4cbc1236c1c0626403995d021d1955 to your computer and use it in GitHub Desktop.
HTML5 Browser Push Notification
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