Zigsaw Consultancy
Zigsaw Consultancy

Reputation: 135

Sending browser notification to specific users (web)

I would want to send out notifications to the list of users from database.

I found a solution to send push notifications:

// request permission on page load
document.addEventListener('DOMContentLoaded', function () {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.'); 
return;
}

 if (Notification.permission !== "granted")
Notification.requestPermission();
});

function notifyMe() {
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('Notification title', {
  icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
  body: "Hey there! You've been notified!",
});

notification.onclick = function () {
  window.open("https://stackoverflow.com/a/13328397/1269037");      
};

}

}

(Source: Chrome desktop notification example ) How can i configure it to send specific notifications to specific users ? Let us assume that I want to send notifications to all users of the table user_notification. I tried using

<?php
$sqx = "SELECT * FROM `user_js` "; 
 ?>

in the code but it does not help either

Upvotes: 3

Views: 4101

Answers (1)

Nir
Nir

Reputation: 1313

step 1 : try adding the following into your HTML response :

<button onclick="notifyMe()">Notify me!</button>

step 2 : if you can see the button and you get a notification when you click on it, try to move on and trigger it as soon as the page loads. see if this link will help you : http://www.w3schools.com/jsref/event_onload.asp

principally, this should work :

<body onload="notifyMe()">

step 3 : if all works well, you'll have to add a PHP condition that will add this "onload=..." only to the users you want.

Upvotes: 2

Related Questions