Skip to content

Instantly share code, notes, and snippets.

@jbalogh
Created January 30, 2012 19:38
Show Gist options
  • Save jbalogh/1706213 to your computer and use it in GitHub Desktop.
Save jbalogh/1706213 to your computer and use it in GitHub Desktop.
push notifications API
var notification = (navigator.notification ||
navigator.mozNotification ||
navigator.webkitNotification);
if (notification) {
// Ask the user to allow notifications.
var request = notification.requestRemotePermission();
request.onsuccess = function() {
var url = request.result;
console.log('New push URL: ' + url);
// We got a new push URL, store it on the server.
jQuery.post('/push-urls/', {url: url});
};
}
var notification = (navigator.notification ||
navigator.mozNotification ||
navigator.webkitNotification);
if (notification) {
// Check if we've asked for permission before without bothering the user.
var check = notification.checkRemotePermission();
check.onsuccess = function() {
var url = request.result;
console.log('Already have a URL: ' + url);
};
check.onerror = function() {
// Request permission if we haven't been denied before.
if (request.error.code != notification.DENIED) {
var request = notification.requestRemotePermission();
request.onsuccess = function() {
var url = request.result;
console.log('New push URL: ' + url);
// We got a new push URL, store it on the server.
jQuery.post('/push-urls/', {url: url});
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment