Notifications API
// check for notifications support
// you can omit the 'window' keyword
if (window.webkitNotifications) {
console.log("Notifications are supported!");
}
else {
console.log("Notifications are not supported for this Browser/OS version yet.");
}
document.querySelector('#show_button').addEventListener('click', function() {
if (window.webkitNotifications.checkPermission() == 0) { // 0 is PERMISSION_ALLOWED
// function defined in step 2
window.webkitNotifications.createNotification(
'icon.png', 'Notification Title', 'Notification content...');
notification_test.ondisplay = function() { ... do something ... };
notification_test.onclose = function() { ... do something else ... };
notification_test.show();
} else {
window.webkitNotifications.requestPermission();
}
}, false);
function checkNotifications(){
$.get( "/path/to/script.php", function( data ) {
// data is the returned response, let's parse the JSON string
json = JSON.parse(data);
// check if any items were returned
if(!$.isEmptyObject(json)){
// send each item to the notify function
for(var i in json){
notifyUser(json[i].title, json[i].options);
}
}
});
setTimeout(checkNotifications, 60000); // call once per minute
}
$(document).ready(checkNotifications);
if ('Notification' in window) {
function notifyUser() {
var title = 'example title';
var options = {
body: 'example body',
icon: 'example icon'
};
if (Notification.permission === "granted") {
var notification = new Notification(title, options);
} else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if (permission === "granted") {
var notification = new Notification(title, options);
}
});
}
}
$('#notify').click(function() {
notifyUser();
return false;
});
} else {
//not happening
}
$num = $_GET['num'];
$db = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if($db) {
mysql_select_db('mydb', $db);
$select = "SELECT alert FROM blog WHERE id = ".$num." && alert = 0 LIMIT 1";
$results = mysql_query($select) or die(mysql_error());
$output = '';
while($row = mysql_fetch_object($results)) {
$output .= "<script>
var title = 'new blog!';
var options = {
body: 'come read my new blog!',
icon: 'same icon as before or maybe a new one!'
};
var notification = new Notification(title, options);
</script>";
$update = "UPDATE blog SET alert = 1 WHERE id = ".$num." && alert = 0 LIMIT 1";
mysql_query($update) or die(mysql_error());
}
echo $output;
}
{
{
title: 'new blog!',
options: {
body: 'come read my new blog!',
icon: 'same icon as before or maybe a new one!'
}
},
{
title: 'another blog item!',
options: {
body: 'come read my second blog!',
icon: 'hooray for icons!'
}
}
}
<button onclick="notifyMe()">Notify me!</button>