fix: per package fixed

This commit is contained in:
2025-12-12 19:27:56 +03:30
parent e5831b86d2
commit 882d26337c
3 changed files with 112 additions and 12 deletions
+34 -11
View File
@@ -47,13 +47,34 @@ public function handle(): int
return Command::SUCCESS; return Command::SUCCESS;
} }
foreach ($reminders as $reminder) { foreach ($reminders as $reminder) {
$user = $reminder->user; $packageName = $reminder->packageName;
$packageName = $reminder->packageName;
if ($reminder->last_repeat?->addDays($reminder->repeat_days)->isFuture()) { // Check if reminder has a specific user
continue; 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( SendPushJob::dispatch(
user: $user, user: $user,
@@ -62,12 +83,14 @@ public function handle(): int
description: $reminder->description ?? '', description: $reminder->description ?? '',
data: $reminder->data ?? [] 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; return Command::SUCCESS;
} }
+8 -1
View File
@@ -36,8 +36,15 @@ public function handle(): void
->withServiceAccount($firebaseFile); ->withServiceAccount($firebaseFile);
$push = $factory->createMessaging(); $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) { if (!$fcmToken) {
return; return;
} }
+70
View File
@@ -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();
}
}