raumstatus/public/js/vendor/notification.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-09-16 13:45:01 +00:00
var Notification = window.Notification || window.mozNotification || window.webkitNotification;
var enabledNotifications,
grantedPermission;
2015-09-16 13:45:01 +00:00
function askPermission () {
Notification.requestPermission(function (permission) {
// console.log(permission);
if (permission == "granted") {
grantedPermission = true;
}
else {
grantedPermission = false;
}
});
}
function toggleNotifications() {
if (enabledNotifications === true) {
enabledNotifications = false;
} else if (!enabledNotifications && grantedPermission === true){
enabledNotifications = true;
} else {
askPermission();
}
}
2015-09-16 13:45:01 +00:00
function showNotification(name, body, icon, timeout) {
if (enabledNotifications) {
var instance = new Notification(
name, {
body: body,
icon: icon
}
);
2015-09-16 13:45:01 +00:00
instance.onclick = function () {
// Something to do
};
instance.onerror = function () {
// Something to do
};
instance.onshow = function () {
setTimeout(function(){
instance.close();
}, timeout * 1000);
};
instance.onclose = function () {
// Something to do
};
}
2015-09-16 13:45:01 +00:00
}