Created
January 30, 2012 19:38
-
-
Save jbalogh/1706213 to your computer and use it in GitHub Desktop.
push notifications API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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