This commit is contained in:
2025-12-12 22:19:54 +03:30
parent 10567e7a81
commit b31837e84d
+74 -10
View File
@@ -24,27 +24,72 @@ public function __construct(
public function handle(): void
{
if (!$firebaseFile = $this->getFirebaseJsonPath()) {
\Log::info("=== Starting SendPushToPackageJob ===");
\Log::info("Package ID: " . $this->packageName->id);
\Log::info("Package Name: " . $this->packageName->name);
\Log::info("Title: " . $this->title);
\Log::info("Description: " . $this->description);
// Check if package exists
if (!$this->packageName) {
\Log::error("PackageName not found!");
return;
}
$firebaseFile = $this->getFirebaseJsonPath();
\Log::info("Firebase file path: " . ($firebaseFile ?: 'NOT FOUND'));
if (!$firebaseFile) {
\Log::warning('Firebase JSON file not found for package: ' . $this->packageName->id);
\Log::warning('Package ID: ' . $this->packageName->id);
// Check if media exists
$media = $this->packageName->getFirstMedia('firebase_json');
if (!$media) {
\Log::warning('No media found with collection name: firebase_json');
} else {
\Log::warning('Media found: ' . $media->file_name);
}
return;
}
// Check if file exists
if (!file_exists($firebaseFile)) {
\Log::error("Firebase JSON file does not exist at path: " . $firebaseFile);
return;
}
try {
$messaging = (new Factory)
->withServiceAccount($firebaseFile)
->createMessaging();
$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('user_package_name.fcm_token')
->get();
\Log::info("Found " . $users->count() . " users with FCM tokens for package " . $this->packageName->id);
if ($users->isEmpty()) {
\Log::warning("No users with FCM tokens found for package: " . $this->packageName->id);
return;
}
foreach ($users as $user) {
$fcmToken = $user->packageNames()
\Log::info("Processing user ID: " . $user->id);
// Get the FCM token from pivot table
$pivot = $user->packageNames()
->where('package_names.id', $this->packageName->id)
->first()
->pivot
->fcm_token;
->pivot;
$fcmToken = $pivot->fcm_token;
\Log::info("User " . $user->id . " FCM Token: " . ($fcmToken ?: 'NOT FOUND'));
if (!$fcmToken) {
\Log::warning("Skipping user " . $user->id . " - no FCM token");
continue;
}
@@ -55,17 +100,36 @@ public function handle(): void
])
->withData($this->data);
$messaging->send($message);
\Log::info("Sending message to user " . $user->id . " with token: " . substr($fcmToken, 0, 20) . "...");
try {
$result = $messaging->send($message);
\Log::info("SUCCESS: Push notification sent to user {$user->id}");
\Log::info("Message ID: " . ($result ?: 'No result returned'));
} catch (\Throwable $e) {
\Log::error("FAILED to send push to user {$user->id}: " . $e->getMessage());
\Log::error("Error details: " . $e->getTraceAsString());
}
}
\Log::info("=== Completed SendPushToPackageJob ===");
} catch (\Throwable $e) {
\Log::error('SendPushToPackageJob failed: ' . $e->getMessage());
\Log::error('Firebase error in SendPushToPackageJob: ' . $e->getMessage());
\Log::error('Stack trace: ' . $e->getTraceAsString());
}
}
private function getFirebaseJsonPath(): ?string
{
$media = $this->packageName->getFirstMedia('firebase_json');
return $media?->getPath();
if (!$media) {
\Log::warning('No media found for collection firebase_json');
return null;
}
$path = $media->getPath();
\Log::info('Media path: ' . $path);
return $path;
}
}