Skip to content

Instantly share code, notes, and snippets.

@zenbakiak
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zenbakiak/aaddbf776513b5a2a704 to your computer and use it in GitHub Desktop.
Save zenbakiak/aaddbf776513b5a2a704 to your computer and use it in GitHub Desktop.
Basic example of HTML5 desktop notifications with jQuery
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