Skip to content

Instantly share code, notes, and snippets.

@argyleink
Created March 12, 2015 20:21
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 argyleink/7b65823e4d60702c01d5 to your computer and use it in GitHub Desktop.
Save argyleink/7b65823e4d60702c01d5 to your computer and use it in GitHub Desktop.
HTML5 Notifications module
app.notify = (function() {
// Determine the correct object to use
var notification = window.Notification || window.mozNotification || window.webkitNotification || false
, permission = false
, note;
function init() {
notification && notification.requestPermission(permissionSuccess);
}
function notify(titleText, bodyText) {
if (!permission) {
alert(titleText + ': ' + bodyText);
return;
}
note = new notification(
titleText,
{
body: bodyText,
dir: 'auto', // or ltr, rtl
lang: 'EN', //lang used within the notification.
tag: 'notificationPopup', //An element ID to get/set the content
icon: '/img/android-icon.png' //The URL of an image to be used as an icon
}
);
listen();
}
function listen() {
note.onclick = function () {
console.log('notification.Click');
}
note.onerror = function () {
console.log('notification.Error');
}
note.onshow = function () {
console.log('notification.Show');
}
note.onclose = function () {
console.log('notification.Close');
}
}
function permissionSuccess() {
permission = true;
notify('Rad!', 'We\'ll keep these to a minimum.')
}
function getPermission() {
return permission;
}
return {
init: init,
say: notify,
supported: getPermission
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment