簡體   English   中英

我希望更新未讀消息的數量,並在用戶收到新消息時播放通知聲音。 我正在使用PHP,MySql,Javascript

[英]i want the number of unread messages to be updated and a notification sound played when a user receives new message. Am using PHP, MySql, Javascript

<!-- Message Notifications -->
        <li class="notifications dropdown">
            <?php
            $total_unread_message_number = 0;
            $current_user = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');

            $this->db->where('sender', $current_user);
            $this->db->or_where('reciever', $current_user);
            $message_threads = $this->db->get('message_thread')->result_array();
            foreach ($message_threads as $row) {
                $unread_message_number = $this->crud_model->count_unread_message_of_thread($row['message_thread_code']);
                $total_unread_message_number += $unread_message_number;
            }
            ?>
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-close-others="true">
                <i class="entypo-mail"></i>
                <?php if ($total_unread_message_number > 0): ?>
                    <span class="badge badge-info"><?php echo $total_unread_message_number; ?></span>

                <!--Javascript message sound notification-->
                <script>
                $(function(){  

                  $('<audio id="chatAudio"><source src="assets/sounds/notify.ogg" type="audio/ogg"><source src="assets/sounds/notify.mp3" type="audio/mpeg"><source    src="assets/sounds/notify.wav" type="audio/wav"></audio>').appendTo('body');;
                  $('#chatAudio')[0].play();

                  });
                </script>

                <?php endif; ?>
            </a>

上面的代碼在標題部分中,因此,只要重新加載頁面(因為有未讀消息),就會播放通知聲音。 但是我希望它通知能夠立即播放,消息進入數據庫。

在這種情況下,您需要調用.php文件,該文件會使用AJAX定期返回未讀消息的數量。

代碼看起來像這樣:

var INTERVAL_IN_MILISECONDS = 5000; // 5s
function checkForUnreadMessages() {
    $.get("getUnreadMessagesCount.php")
    .done(function(unreadMessagesCount) {
        if (unreadMessagesCount > 0) {
            $('<audio id="chatAudio"><source src="assets/sounds/notify.ogg" type="audio/ogg"><source src="assets/sounds/notify.mp3" type="audio/mpeg"><source    src="assets/sounds/notify.wav" type="audio/wav"></audio>').appendTo('body');
            $('#chatAudio')[0].play();
        }
    });
}
checkForUnreadMessages();
setInterval(checkForUnreadMessages, INTERVAL_IN_MILISECONDS);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM