78 lines
2.0 KiB
PHP
78 lines
2.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Modules\RestaurantDelivery\Jobs;
|
||
|
|
|
||
|
|
use Illuminate\Bus\Queueable;
|
||
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||
|
|
use Illuminate\Queue\InteractsWithQueue;
|
||
|
|
use Illuminate\Queue\SerializesModels;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
use Modules\RestaurantDelivery\Services\Firebase\FirebaseService;
|
||
|
|
|
||
|
|
class SendPushNotificationJob implements ShouldQueue
|
||
|
|
{
|
||
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The number of times the job may be attempted.
|
||
|
|
*/
|
||
|
|
public int $tries = 3;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The maximum number of seconds to wait before retrying the job.
|
||
|
|
*/
|
||
|
|
public int $backoff = 10;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a new job instance.
|
||
|
|
*/
|
||
|
|
public function __construct(
|
||
|
|
public readonly string $token,
|
||
|
|
public readonly string $title,
|
||
|
|
public readonly string $body,
|
||
|
|
public readonly array $data = []
|
||
|
|
) {
|
||
|
|
$this->onQueue(config('restaurant-delivery.queue.queues.notifications', 'restaurant-delivery-notifications'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the job.
|
||
|
|
*/
|
||
|
|
public function handle(FirebaseService $firebase): void
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$firebase->sendPushNotification(
|
||
|
|
$this->token,
|
||
|
|
$this->title,
|
||
|
|
$this->body,
|
||
|
|
$this->data
|
||
|
|
);
|
||
|
|
|
||
|
|
Log::info('Push notification sent', [
|
||
|
|
'title' => $this->title,
|
||
|
|
'data' => $this->data,
|
||
|
|
]);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
Log::error('Failed to send push notification', [
|
||
|
|
'title' => $this->title,
|
||
|
|
'error' => $e->getMessage(),
|
||
|
|
]);
|
||
|
|
throw $e;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle a job failure.
|
||
|
|
*/
|
||
|
|
public function failed(\Throwable $exception): void
|
||
|
|
{
|
||
|
|
Log::error('Push notification job failed permanently', [
|
||
|
|
'title' => $this->title,
|
||
|
|
'error' => $exception->getMessage(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|