Created
March 12, 2015 20:21
-
-
Save argyleink/7b65823e4d60702c01d5 to your computer and use it in GitHub Desktop.
HTML5 Notifications module
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
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