2015-09-16 13:45:01 +00:00
|
|
|
var Notification = window.Notification || window.mozNotification || window.webkitNotification;
|
2016-01-06 07:11:55 +00:00
|
|
|
var enabledNotifications,
|
|
|
|
grantedPermission;
|
2015-09-16 13:45:01 +00:00
|
|
|
|
2016-01-06 07:11:55 +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) {
|
2016-01-06 07:28:39 +00:00
|
|
|
if (enabledNotifications) {
|
|
|
|
var instance = new Notification(
|
|
|
|
name, {
|
|
|
|
body: body,
|
|
|
|
icon: icon
|
|
|
|
}
|
|
|
|
);
|
2015-09-16 13:45:01 +00:00
|
|
|
|
2016-01-06 07:28:39 +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
|
|
|
}
|