向用户发送通知


Send notification to users

我试图创建一个功能,一旦按下按钮,它发送一个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语句是错误的,我知道。这是因为我不知道逻辑是什么,所以我可以将条件设置为用户登录,消息由登录用户以外的人发送。(因此,发表评论有效地向所有登录该网站的其他用户发送通知)。

我希望我说得够清楚,你能帮助我。

这只在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>