Skip to content

Instantly share code, notes, and snippets.

@tcelestino
Last active August 29, 2015 14:19
Show Gist options
  • Save tcelestino/558a088efaa1a900fc34 to your computer and use it in GitHub Desktop.
Save tcelestino/558a088efaa1a900fc34 to your computer and use it in GitHub Desktop.
web notification api
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Notification API</title>
</head>
<body>
<script>
var Notify = (function (window, $) {
var app = {
status: null,
interval: null,
getUserPermission: function (permission) {
app.status = permission;
app.startNotification();
},
setPermission: function () {
if ('Notification' in window) {
// Safari
if (window.webkitNotifications && window.webkitNotifications.checkPermission() == 0) {
this.startNotification();
}
window.Notification.requestPermission(this.getUserPermission);
} else {
alert('Seu browser não tem suporte a Notificações');
}
},
authorized: function () {
return this.status === 'granted' || window.Notification.permission === 'granted';
},
setNotification: function (content) {
var notification = new Notification('Notify', {
dir: 'auto',
lang: '',
body: content,
tag: 'r7',
icon: ''
});
notification.onshow = function () {
console.log('exibe');
setTimeout(function () {
notification.close();
}, 10000)
}
},
startNotification: function () {
if (this.authorized()) {
this.setNotification();
}
},
init: function () {
this.setPermission();
}
}
return app;
})(window, jQuery);
(function () {
//var url_json = 'http://localhost:8000/data.json';
Notify.init();
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment