Skip to content

Instantly share code, notes, and snippets.

@1242035
Forked from emilio/notification.js
Created August 8, 2019 14:54
Show Gist options
  • Save 1242035/8aeba9244ea1de1f6b04122494d085a6 to your computer and use it in GitHub Desktop.
Save 1242035/8aeba9244ea1de1f6b04122494d085a6 to your computer and use it in GitHub Desktop.
window.Notification polyfill
/*
* window.Notification polyfill
* @author Emilio Cobos (http://emiliocobos.net)
*/
/* try prefixed */
if( ! window.Notification ) {
window.Notification = (function() {
return window.Notification || window.webkitNotification || window.mozNotification || window.oNotification || window.msNotification;
})()
}
/* request pemission: */
if( window.Notification.permission === 'default' ) {
window.Notification.requestPermission(/* function(permission) {} */);
}
/* If not standard (webkit first implementation) */
if( ! window.Notification && window.webkitNotifications ) {
window.webkitNotifications.requestPermission();
if( window.webkitNotifications.checkPermission() === 0 ) {
window.Notification = (function(){
var notifications_cache = {};
return function(title, options) {
var notification,
tagNotifications,
tagNotification;
options = options || {};
notification = window.webkitNotifications.createNotification(options.iconUrl || options.icon, title, options.body || '');
if( options.onshow ) {
notification.ondisplay = options.onshow;
}
if( options.onclick ) {
notification.onclick = options.onclick;
}
if( options.onclose ) {
notification.onclose = options.onclose;
}
if( options.onerror ) {
notification.onerror = options.onerror;
}
if( options.tag ) {
tagNotifications = notifications_cache[options.tag] = notifications_cache[options.tag] || [];
while( tagNotification = tagNotifications.shift() ) {
tagNotification.close();
}
notifications_cache[options.tag].push(notification);
}
return notification;
}
window.Notification.requestPermission = function(cb) {
return window.webkitNotifications.requestPermission(cb);
}
})()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment