[英]Send push notifications to Taxi drivers but with a delay in a for each loop - php laravel
So this is a simple taxi dispatch server built in php Laravel that sends out push notifications to the matching online Taxi drivers within the radius of the Customer, using a foreach loop:所以这是一个内置在 php Laravel 中的简单出租车调度服务器,它使用 foreach 循环向客户半径内匹配的在线出租车司机发送推送通知:
foreach ($Providers_active as $key => $Provider) {
(new SendPushNotification)->IncomingRequest($Provider->id);
}
I get all the online Drivers in the Customer's radius using this:我使用以下方法获取客户半径内的所有在线驱动程序:
$Providers_active = Provider::with('service')
->select(DB::Raw("(6371 * acos( cos( radians('$latitude') ) * cos( radians(latitude) ) * cos( radians(longitude) - radians('$longitude') ) + sin( radians('$latitude') ) * sin( radians(latitude) ) ) ) AS distance"),'id')
->where('status', 'online')
->orderBy('distance','asc')
->get();
and my IncomingRequest function looks like this:我的IncomingRequest函数如下所示:
public function IncomingRequest($provider){
$provider = Provider::where('id',$provider)->with('profile')->first();
return $this->sendPushToProvider($provider->id, "New Ride Request");
}
But since this code gets executed literally in a milisecond, all of the matching drivers get the Push Notification right at the same time.但是由于这段代码是在几毫秒内执行的,所有匹配的驱动程序都会同时获得正确的推送通知。 But I'm trying to send the push notifications to them one by one with a delay of perhaps a couple of seconds.
但我正在尝试将推送通知一一发送给他们,延迟可能只有几秒钟。
Please note:请注意:
But im pretty sure those are not the best ways to do it.但我很确定这些不是最好的方法。
I would put these into a Job Queue.我会将这些放入作业队列。 You can rate limit how often a job is processed.
您可以对作业的处理频率进行速率限制。 So if you want to send one notification every 10 seconds you could do that.
所以如果你想每 10 秒发送一个通知,你可以这样做。
https://laravel.com/docs/8.x/queues#rate-limiting https://laravel.com/docs/8.x/queues#rate-limiting
Here is some example code they have in the docs.这是他们在文档中的一些示例代码。 This shows
perHour()
but there is also perMinute()
, so if you used perMinute(6)
that should send one every 10 seconds.这显示
perHour()
但也有perMinute()
,所以如果你使用perMinute(6)
应该每 10 秒发送一个。
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
RateLimiter::for('backups', function ($job) {
return $job->user->vipCustomer()
? Limit::none()
: Limit::perHour(1)->by($job->user->id);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.