我正在尝试创建一个功能,按下按钮后,它会向登录该站点的所有用户发送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语句是错误的。那是因为我不知道逻辑是什么,所以我可以将条件设置为用户登录并且消息由登录用户以外的其他人发送。 (因此发布评论会有效地向登录该网站的所有其他用户发送通知。)
我希望我足够清楚,能够帮助我。
答案 0 :(得分: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>