使用 php 和 jquery 进行实时通知
Real time notification by using php and jquery
我有一些活动,每当创建新活动时,我都希望为用户弹出通知,以便他们知道。我想在任何用户创建新事件时生成实时通知我为此使用了 php 和 jQuery 但我面临的问题是我的代码在新事件到达时生成通知但只有一次仅且仅供一个用户使用。我希望每个用户都可以收到有关事件的通知,但如果用户收到该通知则不需要多次通知,则无需再次显示它们,但如果没有,则应生成通知。所以这是我的代码:
Notification.php:-
<?php
$user = "root";
$pass = "";
$host = "localhost";
$dbname = "db_notification";
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e->getMessage();
}
$output = array(
'found' => 0,
'notifications' => array()
);
if ( ! empty($_GET['action'])) {
switch ($_GET['action']) {
case "show_last_five":
$sql = "SELECT * FROM `tbl_notification` INNER JOIN tbl_user_notification ON tbl_user_notification.notification_id = tbl_notification.id WHERE tbl_user_notification.user_id = '1';";
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
if ($q->rowCount() > 0) {
$output['found'] = 0;
$output['notifications'] = null;
} else {
$sql = "SELECT * FROM `tbl_notification` LIMIT 5;";
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
$output['found'] = $q->rowCount();
$tempArr = array();
$insertArray = "";
foreach ($q->fetchAll() as $row) {
array_push($tempArr, $row);
$sql1 = "INSERT INTO tbl_user_notification (user_id,notification_id) VALUES('1','" . $row['id'] . "')";
$q1 = $conn->query($sql1);
}
$output['notifications'] = $tempArr;
}
break;
default:
break;
}
header('Content-Type:application/json;');
echo json_encode($output);
}
?>
Notification.js:-
$(document).ready(function() {
var lastDisplayed = 0;
$('#gnrt_notfctn').on('click', function () {
$.getJSON('http://localhost/test/test/notifications/notification.php? action=show_last_five', function (data) {
var output = '';
if (data['found'] > 0) {
$.each(data['notifications'], function (i, v) {
if ($('#lastNotification').text() !== v['id']) {
var close_button = '';
if (v['close_button'] == 1) {
close_button = "<span style='position:absolute;right:25px;' data- dismiss='alert'><i class='fa fa-close'></i></span>";
} else {
close_button = "";
}
output = output + "<div class='alert alert-" + v['type'] + "'>" +
close_button + "<h4 class='alert_heading'><i class='fa fa-" + v['icon'] + "'> </i>
" + v['heading'] + "</h4>" +
"<p class='alert_body'>" + v['notification_text'] + "</p></div>";
$('#lastNotification').text(v['id']);
}
});
$('#show_notification').html(output);
}
});
});
setInterval(function () {
$('#gnrt_notfctn').click();
}, 3000);
});
设计 API 使其接受用户 ID 和上次收到的通知。
所以在查询中,使用 user_id 和日期时间
过滤通知 table
例如:(考虑 table 中有数据时间字段)
SELECT * FROM tbl_notification INNER JOIN tbl_user_notification ON tbl_user_notification.notification_id = tbl_notification.id WHERE tbl_user_notification.user_id = '1' and tbl_notification.datetime>lastreceiveddatatime
希望有用
我有一些活动,每当创建新活动时,我都希望为用户弹出通知,以便他们知道。我想在任何用户创建新事件时生成实时通知我为此使用了 php 和 jQuery 但我面临的问题是我的代码在新事件到达时生成通知但只有一次仅且仅供一个用户使用。我希望每个用户都可以收到有关事件的通知,但如果用户收到该通知则不需要多次通知,则无需再次显示它们,但如果没有,则应生成通知。所以这是我的代码:
Notification.php:-
<?php
$user = "root";
$pass = "";
$host = "localhost";
$dbname = "db_notification";
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e->getMessage();
}
$output = array(
'found' => 0,
'notifications' => array()
);
if ( ! empty($_GET['action'])) {
switch ($_GET['action']) {
case "show_last_five":
$sql = "SELECT * FROM `tbl_notification` INNER JOIN tbl_user_notification ON tbl_user_notification.notification_id = tbl_notification.id WHERE tbl_user_notification.user_id = '1';";
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
if ($q->rowCount() > 0) {
$output['found'] = 0;
$output['notifications'] = null;
} else {
$sql = "SELECT * FROM `tbl_notification` LIMIT 5;";
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
$output['found'] = $q->rowCount();
$tempArr = array();
$insertArray = "";
foreach ($q->fetchAll() as $row) {
array_push($tempArr, $row);
$sql1 = "INSERT INTO tbl_user_notification (user_id,notification_id) VALUES('1','" . $row['id'] . "')";
$q1 = $conn->query($sql1);
}
$output['notifications'] = $tempArr;
}
break;
default:
break;
}
header('Content-Type:application/json;');
echo json_encode($output);
}
?>
Notification.js:-
$(document).ready(function() {
var lastDisplayed = 0;
$('#gnrt_notfctn').on('click', function () {
$.getJSON('http://localhost/test/test/notifications/notification.php? action=show_last_five', function (data) {
var output = '';
if (data['found'] > 0) {
$.each(data['notifications'], function (i, v) {
if ($('#lastNotification').text() !== v['id']) {
var close_button = '';
if (v['close_button'] == 1) {
close_button = "<span style='position:absolute;right:25px;' data- dismiss='alert'><i class='fa fa-close'></i></span>";
} else {
close_button = "";
}
output = output + "<div class='alert alert-" + v['type'] + "'>" +
close_button + "<h4 class='alert_heading'><i class='fa fa-" + v['icon'] + "'> </i>
" + v['heading'] + "</h4>" +
"<p class='alert_body'>" + v['notification_text'] + "</p></div>";
$('#lastNotification').text(v['id']);
}
});
$('#show_notification').html(output);
}
});
});
setInterval(function () {
$('#gnrt_notfctn').click();
}, 3000);
});
设计 API 使其接受用户 ID 和上次收到的通知。
所以在查询中,使用 user_id 和日期时间
过滤通知 table例如:(考虑 table 中有数据时间字段)
SELECT * FROM tbl_notification INNER JOIN tbl_user_notification ON tbl_user_notification.notification_id = tbl_notification.id WHERE tbl_user_notification.user_id = '1' and tbl_notification.datetime>lastreceiveddatatime
希望有用