fix: send due parameters

This commit is contained in:
2025-12-12 19:59:04 +03:30
parent a49c6f078d
commit 3aab22fe2c
2 changed files with 44 additions and 42 deletions
+6 -14
View File
@@ -70,22 +70,14 @@ public function handle(): int
} }
// Otherwise, send to all users that have this package // Otherwise, send to all users that have this package
else { else {
$users = $packageName->users()->whereNotNull('package_name_user.fcm_token')->get(); // Just dispatch the job once for all users with this package
foreach ($users as $user) {
$fcmToken = $user->packageNames()->where('package_names.id', $packageName->id)->first()->pivot->fcm_token;
if (!$fcmToken) continue;
SendPushToPackageJob::dispatch( SendPushToPackageJob::dispatch(
user: $user, $packageName,
title: $reminder->title, $reminder->title,
packageName: $packageName, $reminder->description ?? '',
description: $reminder->description ?? '', $reminder->data ?? []
data: $reminder->data ?? []
); );
} }
}
$reminder->last_repeat = now(); $reminder->last_repeat = now();
$reminder->save(); $reminder->save();
+19 -9
View File
@@ -1,6 +1,5 @@
<?php <?php
namespace App\Jobs; namespace App\Jobs;
use App\Models\PackageName; use App\Models\PackageName;
@@ -27,11 +26,13 @@ public function handle(): void
{ {
$firebaseFile = $this->getFirebaseJsonPath(); $firebaseFile = $this->getFirebaseJsonPath();
if (!$firebaseFile) { if (!$firebaseFile) {
\Log::warning('Firebase JSON file not found for package: ' . $this->packageName->id);
return; return;
} }
try {
$factory = (new Factory)->withServiceAccount($firebaseFile); $factory = (new Factory)->withServiceAccount($firebaseFile);
$push = $factory->createMessaging(); $messaging = $factory->createMessaging();
// Get all users that have this package AND have an FCM token // Get all users that have this package AND have an FCM token
$users = $this->packageName->users() $users = $this->packageName->users()
@@ -49,16 +50,25 @@ public function handle(): void
continue; continue;
} }
$message = CloudMessage::fromArray([ $message = CloudMessage::withTarget('token', $fcmToken)
'token' => $fcmToken, ->withNotification([
'notification' => [
'title' => $this->title, 'title' => $this->title,
'body' => $this->description, 'body' => $this->description,
], ])
'data' => $this->data, ->withData($this->data);
]);
$push->send($message); try {
$messaging->send($message);
\Log::info("Push notification sent to user {$user->id} for package {$this->packageName->id}");
} catch (\Throwable $e) {
\Log::error("Failed to send push to user {$user->id}: " . $e->getMessage());
}
}
\Log::info("Sent push notifications to " . $users->count() . " users for package: " . $this->packageName->id);
} catch (\Throwable $e) {
\Log::error('Firebase error in SendPushToPackageJob: ' . $e->getMessage());
} }
} }