hello everyone im trying to when new comment added to the database , javascript will send notification and here is my code for ajax.php
<?php
require_once "config.php";
$id = $_GET['id'];
$q = $_GET['q'];
$totalComments = 0;
$count = $db->count('comments', [
"id[<]" => $id,
'OR' => [
"name[~]" => $q,
"comment[~]" => $q,
],
]);
$totalComments += $count;
// Check if there are new comments
if ($totalComments!== $count) {
// Instead, return a flag to indicate new comments
$newComments = true;
} else {
$newComments = false;
}
$result = getComment2($db, $id, $q?? '');
echo json_encode([
"count" => $count,
"data" => $count > 0? array_values($result) : null,
"newComments" => $newComments, // Add a flag to indicate new comments
]);
and also here its my notification.js file
window.addEventListener('load', () => {
if (!window.Notification) return;
Notification.requestPermission().then(permission => {
if (permission === 'granted') {
// Listen for new comments flag from PHP script
document.addEventListener('ajaxComplete', event => {
const xhr = event.detail.xhr;
const response = JSON.parse(xhr.responseText);
if (response.newComments) {
sendNotification();
}
});
}
});
});
const sendNotification = () => {
let notification = new Notification('Yeni yorum', {
body: 'Yeni yorum yapıldı. ',
icon: '#icon yolu',
title: 'Çiçek sepeti yorum'
});
};
when i try add new comment, nothing happens can someone help me ?