Skip to content

Instantly share code, notes, and snippets.

@williampsena
Last active March 28, 2016 01:41
Show Gist options
  • Save williampsena/a4758fa48b967b0c04f5 to your computer and use it in GitHub Desktop.
Save williampsena/a4758fa48b967b0c04f5 to your computer and use it in GitHub Desktop.
function RequestPermission(callback){
callback = callback || function(status) {
console.log('Status da permissão: ' + status);
callback(status === "granted");
};
Notification.requestPermission(callback);
}
function SendNotification(message, callback){
if(Notification.permission === 'default') {
RequestPermission();
return;
}
callback = callback = {};
if(typeof message !== 'object'|| !message.body || !message.title){
throw new Error('Invalid message!');
return;
}
var notification = new Notification(message.title, {
body: message.body,
tag: message.tag,
icon: message.icon
});
notification.onshow = callback.show || function show() {
console.log('OnShow: Yeah! Notification success');
};
notification.onclick = callback.click || function click() {
console.log('OnClick: Notification clicked');
};
notification.onclose = callback.close || function close() {
console.log('OnClose: Notification closed');
};
notification.onerror = callback.error || function error() {
console.log('OnError: Unexpected error');
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment