fix: per package fixed
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\PackageName;
|
||||
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 SendPushToPackageJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public PackageName $packageName,
|
||||
public string $title,
|
||||
public string $description = '',
|
||||
public array $data = []
|
||||
) {}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$firebaseFile = $this->getFirebaseJsonPath();
|
||||
if (!$firebaseFile) {
|
||||
return;
|
||||
}
|
||||
|
||||
$factory = (new Factory)->withServiceAccount($firebaseFile);
|
||||
$push = $factory->createMessaging();
|
||||
|
||||
// Get all users that have this package AND have an FCM token
|
||||
$users = $this->packageName->users()
|
||||
->whereNotNull('package_name_user.fcm_token')
|
||||
->get();
|
||||
|
||||
foreach ($users as $user) {
|
||||
$fcmToken = $user->packageNames()
|
||||
->where('package_names.id', $this->packageName->id)
|
||||
->first()
|
||||
->pivot
|
||||
->fcm_token;
|
||||
|
||||
if (!$fcmToken) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$message = CloudMessage::fromArray([
|
||||
'token' => $fcmToken,
|
||||
'notification' => [
|
||||
'title' => $this->title,
|
||||
'body' => $this->description,
|
||||
],
|
||||
'data' => $this->data,
|
||||
]);
|
||||
|
||||
$push->send($message);
|
||||
}
|
||||
}
|
||||
|
||||
private function getFirebaseJsonPath(): ?string
|
||||
{
|
||||
$media = $this->packageName->getFirstMedia('firebase_json');
|
||||
return $media?->getPath();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user