135 lines
4.8 KiB
PHP
135 lines
4.8 KiB
PHP
<?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
|
|
{
|
|
\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 {
|
|
$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) {
|
|
\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;
|
|
|
|
$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;
|
|
}
|
|
|
|
$message = CloudMessage::withTarget('token', $fcmToken)
|
|
->withNotification([
|
|
'title' => $this->title,
|
|
'body' => $this->description,
|
|
])
|
|
->withData($this->data);
|
|
|
|
\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('Firebase error in SendPushToPackageJob: ' . $e->getMessage());
|
|
\Log::error('Stack trace: ' . $e->getTraceAsString());
|
|
}
|
|
}
|
|
|
|
private function getFirebaseJsonPath(): ?string
|
|
{
|
|
$media = $this->packageName->getFirstMedia('firebase_json');
|
|
if (!$media) {
|
|
\Log::warning('No media found for collection firebase_json');
|
|
return null;
|
|
}
|
|
|
|
$path = $media->getPath();
|
|
\Log::info('Media path: ' . $path);
|
|
return $path;
|
|
}
|
|
} |