Himanshu
Himanshu

Reputation: 281

Real time notification by using php and jquery

I have events in which when ever new event is created I want notification to pop up for users so that they can know. I want to generate real time notification when ever any user create new event I have used php and jQuery for that but the problem I'm facing is my code is generating notification when new event arrives but for once only and for one user only. I want that every user can have notification about events but not multiple times if user had that notification then no need to show them again but if not then notification should be generated. So Here is my code:

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>
                            &nbsp;" + 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);
});

Upvotes: 2

Views: 1992

Answers (1)

Manju
Manju

Reputation: 728

Design the API in such a way that it will accept the user id and last time notification received.

so in the query, filter the notification table using user_id and date time

for example:(consider there is data-time field in the 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

Hope this is useful

Upvotes: 4

Related Questions