Skip to content

Instantly share code, notes, and snippets.

@argyleink

argyleink/notify.js

Created Mar 12, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.