getFirebaseJsonPath(); if (!$firebaseFile) { \Log::warning('Firebase JSON file not found for package: ' . $this->packageName->id); return; } 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(); foreach ($users as $user) { $fcmToken = $user->packageNames() ->where('package_names.id', $this->packageName->id) ->first() ->pivot ->fcm_token; 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()); } } \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()); } } private function getFirebaseJsonPath(): ?string { $media = $this->packageName->getFirstMedia('firebase_json'); return $media?->getPath(); } }