I have MySQL database which is updating every second or microsecond. using notification table I have to find a correct user to send a push notification, there are several types of the notification. so not all notification send to all user's instead I have to check which user subscribe to which notification and based on that I'm sending a notification to users, so far it's working correctly but not accurate.
My program logic is:
First getting all new notification from notification table which is pending to send.
$newnotification = get_all_newnotification();
Then processing notification to check is there any user subscribe to this notification? if yes then sending to push to that user using Firebase
foreach ($newnotification as $data) {
$findusers = mysqli($connection,"SELECT U.device_token FROM user_notification as UN INNER JOIN user as U on U.id = UN.user_id WHERE UN.notification_type = '{$data['type']}' ");
$user_token = [];
while($result = mysqli_fetch_assoc()) {
$user_token[] = $result['device_token'];
}
if(count($user_token) > 0) {
$firebase = new Firebase();
$notification_data['title'] = 'Title here';
$notification_data['description'] = 'Description goes here';
$firebase->send($user_token,$notification_data);
}
//here I'm updating notification sent flag in table so won't get next time
}
So this working but not as required, because while above program finished there are many new notifications inserted in the table, then user will receive notification delay of 5 to 10 minutes (depends on above program execution time)
Does anyone know how to send notification as soon as possible?