我编写了一些代码,这些代码应该在特定情况下在特定用户上线时发送推送通知。 我需要帮助优化和修复它,因为它有时只能工作,我相信它对我的服务器造成了相当大的影响。
这个工作的系统是一个票务系统。当分配新票证时,假设向具有管理员权限的任何用户发送推送通知,然后当用户被“分配”给票证时,它还应该向他们发送推送通知。我有一些问题,程序向同一个用户发送多个通知,但能够修复它,但现在它只发送它们somtimes(我无法找到它实际工作的模式)
这是我的代码:
Javascript,实际的通知:
<script>
function notifyMe() {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('You\'ve been added to a ticket!', {
icon: 'images/trg.jpg',
body: "You have just been added to a ticket, please check the ticket system for more details.",
priority: "2",
});
notification.onclick = function () {
window.open("http://example");
};
}
}
</script>
<script>
function notifyAdmin() {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('A new ticket has been added!', {
icon: 'images/trg.jpg',
body: "A new ticket has been added!",
priority: "2",
});
notification.onclick = function () {
window.open("http://example");
};
}
}
</script>
我的php,所有if语句以及何时发送等等。通知是通过函数发送的(sendNotify()),它实际上只是从之前调用JavaScript。
$configs = include("config.php");
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$dsn = 'mysql:dbname='.$configs['SQL-Database'].';host='.$configs['SQL-Host'].'';
$user = $configs['SQL-User'];
$password = $configs['SQL-Pass'];
$con = mysqli_connect($configs['SQL-Host'], $configs['SQL-User'], $configs['SQL-Pass'], $configs['SQL-Database']) or die("Error " . mysqli_error($con));
$checkTickets = mysqli_query($con, "SELECT * FROM tickets WHERE notified='0'");
$tcheckTickets = mysqli_num_rows($checkTickets);
if(($tcheckTickets > 0) && ($_SESSION['Level'] >= 2)) {
try {
$db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$st = $db->prepare('SELECT UniqueID FROM tickets WHERE notified=0');
$st->execute();
$result = $st->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $d){
echo $d['UniqueID'] . "<br/>";
$id = $d['UniqueID'];
$st = $db->prepare("UPDATE tickets SET notified=1 WHERE UniqueID=$id");
sendSound();
sendNotifyAdmin();
sleep(3);
$st->execute();
}
}
$checkTickets2 = mysqli_query($con, "SELECT * FROM tickets WHERE notified='1'");
$tcheckTickets2 = mysqli_num_rows($checkTickets2);
if($tcheckTickets2 > 0) {
try {
$db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$st = $db->prepare('SELECT * FROM tickets WHERE notified=1');
$st->execute();
$result = $st->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $d){
if(strcmp($d['Assigned'],$_SESSION['Name']) == 0) {
echo $d['UniqueID'] . "<br/>";
$id = $d['UniqueID'];
$st = $db->prepare("UPDATE tickets SET notified=2 WHERE UniqueID=$id");
sendSound();
sendNotify();
sleep(3);
$st->execute();
}
}
}
目前的工作原理如下:
提交故障单时,数据库中“已通知”列中的值为0。我之前输入的脚本每隔几秒刷新一次,并首先检查notified
等于0的任何票证,如果找到任何票证,它应该通过管理员权限向在线任何用户发送通知。然后,脚本检查任何等于1的notified
,并向分配给故障单的任何用户发送消息,然后在notified
等于2时停止。
我绝对愿意接受任何使这更容易,负载更少且实际工作正常的系统。我也欢迎提出如何改进现有代码以便更好地工作的建议。