Last active
August 29, 2015 14:16
-
-
Save zenbakiak/aaddbf776513b5a2a704 to your computer and use it in GitHub Desktop.
Basic example of HTML5 desktop notifications with jQuery
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
window.Notify = { | |
notification: null, | |
defaults: { title: '', type: 'autohide' }, | |
isSupported: function(){ | |
if (!Notification) { | |
console.log('Please install Chrome, Firefox, Opera or any decent browser.'); | |
return false; | |
} | |
if (Notification.permission !== "granted"){ | |
Notification.requestPermission(); | |
} | |
else{ | |
return; | |
} | |
}, | |
// notification: { icon: «icon_url», title: «string just for firefox», body: «content», tag: «id», dir: [auto/ltr/rtl] } | |
// event_handlers: { onclick: callback(), onshow, onerror, onclose } | |
create: function(notification, the_url){ | |
var settings = $.extend({}, this.defaults, notification); | |
this.notification = new Notification(settings.title, settings); | |
this.notification.onclick = function(){ | |
window.open = the_url; | |
} | |
if (settings.type == 'autohide') { | |
setTimeout(function(){ | |
Notify.cancel(); | |
}, 4000); | |
}; | |
}, | |
cancel: function(id){ | |
this.notification.close(); | |
} | |
} | |
$(function(){ | |
// look for privileges | |
Notify.isSupported(); | |
Notify.create({ | |
icon: '/media/images/logo/fw-apple.png', | |
title: 'Notification title', | |
body: 'Hey there pal, Just want to show you a basic implementation of desktop notifications.', | |
tag: 'notify-123', | |
type: 'fixed' | |
}, 'https://www.facebook.com/'); | |
// use this function to clear | |
// Notify.cancel(); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment