HTML5 Notification With PHP <p>I noticed Facebook started using the HTML5 notification for desktop and I thought I would start dabbling in it for fun for my blog. My idea is pretty simple: new blog comes out, apache cronjob runs every X minutes and calls a file, does some PHP wizardry and out goes the notification. </p> <p>I have looked online and found examples using <a href="https://codeforgeek.com/2014/10/desktop-notification-like-facebook-using-html5/">node.js and angular</a>, but I'm not comfortable using either of those so I'd rather stick with PHP. </p> <p>Here is my process: The user goes to my blog and will click a button to allow notifications. For brevity, the below code sends the users a notification when they click the "notify" button. This works perfectly, and in theory should subscribe them to any future notifications. </p> <pre><code>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 } </code></pre> <p>You can see the <a href="https://jsfiddle.net/qdb1d414/">fiddle</a> of the above. </p> <p>Access to the user is granted and now I should be able to send them notifications whenever I want. Awesome! I then write up a blog entry and it has the ID of XYZ. My cronjob goes and calls the following PHP script, using the above node.js example as a template.</p> <p>(In this example I am just calling the script manually from my phone and watching my desktop screen. Since my desktop is "subscribed" to the same domain, I think the following would/should work.)</p> <pre><code>$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." &amp;&amp; alert = 0 LIMIT 1"; $results = mysql_query($select) or die(mysql_error()); $output = ''; while($row = mysql_fetch_object($results)) { $output .= "&lt;script&gt; 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); &lt;/script&gt;"; $update = "UPDATE blog SET alert = 1 WHERE id = ".$num." &amp;&amp; alert = 0 LIMIT 1"; mysql_query($update) or die(mysql_error()); } echo $output; } </code></pre> <p>I then check the database and blog entry XYZ's "alert" is now set to "1", yet my desktop browser never got notified. Since my browser is subscribed to the same URL that is pushing out the notification, I would imagine I'd get a message. </p> <p>Either I'm doing something wrong (perhaps PHP isn't the right language for this?), or I'm misunderstanding the spec. Could somebody help point me in the right direction? I think I'm missing something. </p> <p>Thanks a lot. </p> <p><strong>Update 1</strong></p> <p>According to the comments, if I just call a script with this in it:</p> <pre><code> 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); </code></pre> <p>It should hit all devices that are subscribed to my notifications. I tried this on my phone but my desktop still didn't get a notification. I still think I'm missing something as my notifications seem stuck to one device and can only be called on page-load or on click as opposed to Facebook which sends you notifications even if the page isn't open in your browser. </p>