How to setup notification sound and "orange blinking tab"

Viewed 119

I'm trying to make a notification to my simple php/js chat:

First, I'm trying to compare my log.html (which containing the messages):

my original post.php ( //jQuery request. It posts the client's input and data being sent to the post.php file each time the user submits the form and sends a new message.) The. post.php is saving the messages into log.html file.

 <?php
 session_start();

 if(isset($_SESSION['name'])){
    $text = $_POST['text'];
    $text_message = "<div class='msgln'><span class='chat-time'>".date("F j, g:i A")."</span> <b class='user-name'>".$_SESSION['name']."</b> ".stripslashes(htmlspecialchars($text))."<br></div>";

    file_put_contents("log.html", $text_message, FILE_APPEND | LOCK_EX);
}
?>

my edited post.php (trying to set up a function, which is checking the "new" message - comparing with old ones).

<?php
session_start();
if(isset($_SESSION['name'])){
    $text = $_POST['text'];     
 
    $text_message = "<div class='msgln'><span class='chat-time'>".date("F j, g:i A")."</span> <b class='user-name'>".$_SESSION['name']."</b> ".stripslashes(htmlspecialchars($text))."<br></div>";

    // Check if content is different

    function text_check(){
        $ah = fopen("log.html", 'rb');
        $aha = preg_match(fread($ah, 8192), $text_message);
        $result = true;

        while(!feof($ah)) {
            if( $aha === false){
                $result = false;
                break;
            }
        }
        fclose($ah);
        return $result;    
    }

file_put_contents("log.html", $text_message, FILE_APPEND | LOCK_EX);    
}
?>

If post.php function "text_check()" return true in my index.php:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">

function checkUpdate()
{
    $.post("post.php", function(text_check){
        if (text_check.toString()=="true") {
            playSound();
        }
    });
}

function playSound()
{
    var audio = new Audio('my_audio');
    audio.play();
}

Is it a OK conception?

And how can I make an "orang-red blinking" notification, when browser is minimized?

I read something about title - have to change it. Will it work, if I change it every second with JS?

0 Answers
Related