douzhan2027 2016-02-18 17:04
浏览 194
已采纳

我希望更新未读消息的数量,并在用户收到新消息时播放通知声音。 我使用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>

The above code is in the header section, hence the notification sound is played when ever the page is reloaded as lond that there is a unread message. But i want it the notification to play immediately the message enters the database.

  • 写回答

1条回答 默认 最新

  • dsfsdf7852 2016-02-18 17:16
    关注

    In that case you need to call a .php file which returns the number of unread messages periodically with AJAX.

    The code would look something like this:

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目