2016-10-07 42 views
-1

我试图创建一个功能,一旦按下按钮,它会向网站上登录的所有用户发送一个chrome通知,除了按下按钮的用户以外,它们存储在用户数据库中的表。现在,为了简单起见,我已经做了一个确认按钮被单击的警报,所以通知还没有写入。我拥有的代码如下。发送通知给用户

<!DOCTYPE html> 
    <html> 
    <head> 
     <title></title> 

</head> 
<body> 
    <div class="button"> 
     <form method="post"> 
      <input name="send" type="hidden" value="id"> <input name="send" 
      type="submit" value="knop"> 
     </form> 
    </div> 

//if the 'send' button is clicked, it echoes a script saying the button is clicked. 
if (isset($_POST['send'])) 
    { 
     echo "<script>alert(\"the button is clicked\")</script>"; 
// if the username is logged in AND the message is sent it should send the code script alert to all existing users in the 'user' table in the database. 

     if (isset($_SESSION['username']) AND ('$_POST[id]', '$_POST[name]', '$_POST[message]') 
    { 
echo "<script>alert(\"yes\")</script>"; 
} 
} 
    </body> 
    </html> 

最后一条if语句是错误的,我知道。这是因为我不知道逻辑是什么,所以我可以将条件设​​置为用户已登录,并且该消息是由登录用户以外的人员发送的。 (所以发布评论有效地发送通知给所有其他登录在网站上的用户)。

我希望我很清楚你能帮助我。

+1

那么,如果它是活着的通知,你将需要使用websockets .. – Naruto

+0

谢谢,我不知道。你能详细说明一下吗? – Zhyohzhy

+0

您说过“所有用户都注册并存储在用户表中”和“所有其他用户都登录了该站点” - 您是指哪一个? – Quentin

回答

0

这仅适用于local切勿使用database进行测试。

这是Notification的示例代码。您只需在编辑器中复制并粘贴,并将其保存为.php扩展名。该文件必须放置在www/。并运行它。变量var articles保存推送通知的数据。并且setTimeout(function(){这个函数被称为timing用于两个不同的通知。

<?php 
$var; 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<title>Display browser Notification from Web Application Demo</title> 
<script type="text/javascript"> 

var articles = [ 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"] 
]; 


setTimeout(function(){ 
var x = Math.floor((Math.random() * 10) + 1); 
var title=articles[x][0]; 
var desc='Most popular article.'; 
var url=articles[x][1]; 
notifyBrowser(title,desc,url); 
}, 200000); 



document.addEventListener('DOMContentLoaded', function() 
{ 

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

document.querySelector("#notificationButton").addEventListener("click", function(e) 
{ 
var x = Math.floor((Math.random() * 10) + 1); 
var title=articles[x][0]; 
var desc='Most popular article.'; 
var url=articles[x][1]; 
notifyBrowser(title,desc,url); 
e.preventDefault(); 
}); 

//=================================== 
setInterval(function(e){ 
var x = Math.floor((Math.random() * 10) + 1); 
var title=articles[x][0]; 
var desc='Most popular article.'; 
var url=articles[x][1]; 
notifyBrowser(title,desc,url); 
e.preventDefault(); 

}, 5000); 
//=================================== 
}); 

function notifyBrowser(title,desc,url) 
{ 
if (!Notification) { 
console.log('Desktop notifications not available in your browser..'); 
return; 
} 
if (Notification.permission !== "granted") 
{ 
Notification.requestPermission(); 
} 
else { 
var notification = new Notification(title, { 
icon:'https://cdn0.iconfinder.com/data/icons/basic-ui-elements-colored/700/09_bell-3-512.png', 
body: desc, 
}); 

// Remove the notification from Notification Center when clicked. 
notification.onclick = function() { 
window.open(url);  
}; 

// Callback function when the notification is closed. 
notification.onclose = function() { 
console.log('Notification closed'); 
}; 

} 
} 


</script> 



<style type="text/css"> 
.hover{background-color: #cc0000} 
#container{ margin:0px auto; width: 800px} 
.button { 
font-weight: bold; 
    padding: 7px 9px; 
    background-color: #5fcf80; 
    color: #fff !important; 
    font-size: 12px; 
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; 
    cursor: pointer; 
    text-decoration: none; 
    text-shadow: 0 1px 0px rgba(0,0,0,0.15); 
    border-width: 1px 1px 3px !important; 
    border-style: solid; 
    border-color: #3ac162; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
    display: -moz-inline-stack; 
    display: inline-block; 
    vertical-align: middle; 
    zoom: 1; 
    border-radius: 3px; 
    box-sizing: border-box; 
    box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset; 
} 
.authorBlock{border-top:1px solid #cc0000;} 
</style> 
</head> 
<body> 
<div id="container"> 
<h1>Display browser Notification from Web Application Demo</h1> 

<h4>Click notification button</h4> 
<a href="#" id="notificationButton" class="button">Notification</a> 
</div> 


</body> 
</html> 
+1

由于该问题基本上是关于服务器推送的,因此提供客户端替代'alert'并不是真的有帮助。 – Quentin

+0

好吧谢谢,可能是他尝试转换成服务器推送。我只是想一个主意。 bcz他不知道逻辑@Quentin – Karthi

+1

OP确实知道逻辑。他们只是使用警报而不是通知API。问题在于为所有用户触发,而不是通知用户界面。 – Quentin