fix: per package fixed
This commit is contained in:
@@ -48,12 +48,33 @@ public function handle(): int
|
||||
}
|
||||
|
||||
foreach ($reminders as $reminder) {
|
||||
$user = $reminder->user;
|
||||
$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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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