I've been working on a custom web application for a while in which the user can add a reminder and then choose when to be notified. Basically when the current time matches the time chosen by the user, they should then get a push notification. I've set up a cron job, where the php file is run every minute, but the push notifications simply do not work.
$curDate= date('Y-m-d');
date_default_timezone_set("Europe/Athens");
$curTime= date('H:i:s');
$reminders = $pdo->prepare("SELECT * FROM con_reminders ORDER BY id desc");
$reminders->execute();
$reminders = $reminders->fetchAll();
foreach($reminders as $reminder){
$event_id = $reminder['event_id'];
$event_time = $reminder['event_time'];
$event_time_1 = $reminder['event_time_1'];
$event_time_2 = $reminder['event_time_2'];
$event_time_3 = $reminder['event_time_3'];
$event_date = $reminder['event_date'];
$event_notes = $reminder['event_notes'];
if ( $curDate == $event_date && $curTime == $event_time ){
?>
<script>
function notifyMe() {
if (!("Notification" in window)) {
alert("The browser does not support desktop notifications!");
}
else if (Notification.permission === "granted") {
let notification = new Notification("Hello!!");
}
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
if (permission === "granted") {
let notification = new Notification("Hello!!");
}
});
}
</script>
<?php } ?>
I don't know if the problem lies in this code or I'm completely missing something else entirely. Any help would be really appreciated.
$curTimewill be executed at exactly the second a notification is scheduled, which would only then trigger the JavaScript you’re outputting to the client? Hint: unless you’re actively attempting to execute this script as such, it’s extremely low. You seem to have a very fundamental misunderstanding of how this infrastructure works in practice. - esqew