Files
approagency/app/Jobs/SendPushJob.php
T
2025-09-10 15:28:46 +03:30

65 lines
1.6 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\PackageName;
use App\Models\Reminder;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\CloudMessage;
class SendPushJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
public User $user,
public $title,
public PackageName $packageName,
public string $description = '',
public array $data = []
) {}
/**
* Execute the job.
*/
public function handle(): void
{
$firebaseFile = $this->getFirebaseJsonPath();
$factory = (new Factory)
->withServiceAccount($firebaseFile);
$push = $factory->createMessaging();
$message = CloudMessage::fromArray([
'token' => $this->user->packageNames()->where('package_names.id', $this->packageName->id)->first()->pivot->fcm_token,
'notification' => [
'title' => $this->title,
'body' => $this->description,
],
'data' => $this->data
]);
$push->send($message);
}
/**
* Get Firebase JSON file path from Media Library for the package.
*/
private function getFirebaseJsonPath(): ?string
{
$media = $this->packageName->getFirstMedia('firebase_json');
if (!$media) {
return null;
}
return $media->getPath();
}
}