首页 > 解决方案 > 每微秒发送一次推送通知

问题描述

我有 MySQL 数据库,它每秒或微秒更新一次。使用通知表我必须找到正确的用户来发送推送通知,通知有几种类型。因此,并非所有通知都发送给所有用户,而是我必须检查哪个用户订阅了哪个通知,并据此向用户发送通知,到目前为止它工作正常但不准确。

我的程序逻辑是:

首先从待发送的通知表中获取所有新通知。

$newnotification  = get_all_newnotification();

然后处理通知以检查是否有任何用户订阅此通知?如果是,则使用 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
}

所以这个工作但不是按要求的,因为当上述程序完成时,表中插入了许多新通知,然后用户将收到 5 到 10 分钟的通知延迟(取决于上述程序执行时间)

有谁知道如何尽快发送通知?

标签: phpmysqlfirebasepush-notification

解决方案


推荐阅读