diff --git a/app/Console/Commands/SendDueReminders.php b/app/Console/Commands/SendDueReminders.php index 464f96f..642af56 100644 --- a/app/Console/Commands/SendDueReminders.php +++ b/app/Console/Commands/SendDueReminders.php @@ -47,13 +47,34 @@ public function handle(): int return Command::SUCCESS; } - foreach ($reminders as $reminder) { - $user = $reminder->user; - $packageName = $reminder->packageName; + foreach ($reminders as $reminder) { + $packageName = $reminder->packageName; - if ($reminder->last_repeat?->addDays($reminder->repeat_days)->isFuture()) { - continue; - } + // Check if reminder has a specific user + if ($reminder->user_id) { + $user = $reminder->user; + + $relation = $user->packageNames()->where('package_names.id', $packageName->id)->first(); + $fcmToken = $relation?->pivot?->fcm_token; + + if ($fcmToken) { + SendPushJob::dispatch( + user: $user, + title: $reminder->title, + packageName: $packageName, + description: $reminder->description ?? '', + data: $reminder->data ?? [] + ); + } + } + // 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; SendPushJob::dispatch( user: $user, @@ -62,12 +83,14 @@ public function handle(): int description: $reminder->description ?? '', data: $reminder->data ?? [] ); - - $reminder->last_repeat = $now; - $reminder->save(); - - $this->info("Queued push notification for reminder {$reminder->id}."); } + } + + $reminder->last_repeat = now(); + $reminder->save(); + $this->info("Queued push notification for reminder {$reminder->id}."); +} + return Command::SUCCESS; } diff --git a/app/Jobs/SendPushJob.php b/app/Jobs/SendPushJob.php index 0ce221e..e533491 100644 --- a/app/Jobs/SendPushJob.php +++ b/app/Jobs/SendPushJob.php @@ -36,8 +36,15 @@ public function handle(): void ->withServiceAccount($firebaseFile); $push = $factory->createMessaging(); - $fcmToken = $this->user->packageNames()->where('package_names.id', $this->packageName->id)->first()->pivot->fcm_token; + $relation = $this->user->packageNames() + ->where('package_names.id', $this->packageName->id) + ->first(); + if (!$relation || !$relation->pivot?->fcm_token) { + return; // skip if no token + } + + $fcmToken = $relation->pivot->fcm_token; if (!$fcmToken) { return; } diff --git a/app/Jobs/SendPushToPackageJob.php b/app/Jobs/SendPushToPackageJob.php new file mode 100644 index 0000000..34e7677 --- /dev/null +++ b/app/Jobs/SendPushToPackageJob.php @@ -0,0 +1,70 @@ +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(); + } +}