From 3aab22fe2c66cadffeadcd36e9eaaecd3bd11b24 Mon Sep 17 00:00:00 2001 From: AmirmahdiNourkazemi Date: Fri, 12 Dec 2025 19:59:04 +0330 Subject: [PATCH] fix: send due parameters --- app/Console/Commands/SendDueReminders.php | 24 +++------ app/Jobs/SendPushToPackageJob.php | 62 +++++++++++++---------- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/app/Console/Commands/SendDueReminders.php b/app/Console/Commands/SendDueReminders.php index e37a3c2..0eed954 100644 --- a/app/Console/Commands/SendDueReminders.php +++ b/app/Console/Commands/SendDueReminders.php @@ -70,22 +70,14 @@ public function handle(): int } // Otherwise, send to all users that have this package else { - $users = $packageName->users()->whereNotNull('package_name_user.fcm_token')->get(); - - foreach ($users as $user) { - $fcmToken = $user->packageNames()->where('package_names.id', $packageName->id)->first()->pivot->fcm_token; - - if (!$fcmToken) continue; - - SendPushToPackageJob::dispatch( - user: $user, - title: $reminder->title, - packageName: $packageName, - description: $reminder->description ?? '', - data: $reminder->data ?? [] - ); - } - } + // Just dispatch the job once for all users with this package + SendPushToPackageJob::dispatch( + $packageName, + $reminder->title, + $reminder->description ?? '', + $reminder->data ?? [] + ); +} $reminder->last_repeat = now(); $reminder->save(); diff --git a/app/Jobs/SendPushToPackageJob.php b/app/Jobs/SendPushToPackageJob.php index 34e7677..e2ba52a 100644 --- a/app/Jobs/SendPushToPackageJob.php +++ b/app/Jobs/SendPushToPackageJob.php @@ -1,6 +1,5 @@ getFirebaseJsonPath(); if (!$firebaseFile) { + \Log::warning('Firebase JSON file not found for package: ' . $this->packageName->id); return; } - $factory = (new Factory)->withServiceAccount($firebaseFile); - $push = $factory->createMessaging(); + try { + $factory = (new Factory)->withServiceAccount($firebaseFile); + $messaging = $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(); + // 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; + foreach ($users as $user) { + $fcmToken = $user->packageNames() + ->where('package_names.id', $this->packageName->id) + ->first() + ->pivot + ->fcm_token; - if (!$fcmToken) { - continue; + if (!$fcmToken) { + continue; + } + + $message = CloudMessage::withTarget('token', $fcmToken) + ->withNotification([ + 'title' => $this->title, + 'body' => $this->description, + ]) + ->withData($this->data); + + 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()); + } } - $message = CloudMessage::fromArray([ - 'token' => $fcmToken, - 'notification' => [ - 'title' => $this->title, - 'body' => $this->description, - ], - 'data' => $this->data, - ]); - - $push->send($message); + \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()); } } @@ -67,4 +77,4 @@ private function getFirebaseJsonPath(): ?string $media = $this->packageName->getFirstMedia('firebase_json'); return $media?->getPath(); } -} +} \ No newline at end of file