From 07f22972eea5872ce593c06f8a1fddd20fb34d6e Mon Sep 17 00:00:00 2001 From: root Date: Thu, 28 May 2026 16:31:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(reliability):=20resolve=20F-09=20=E2=80=94?= =?UTF-8?q?=20add=20error=20handling=20for=20FCM=20credential=20loading?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/Services/FcmService.php | 38 ++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/app/Services/FcmService.php b/src/app/Services/FcmService.php index 15962e46..24cb6eb2 100644 --- a/src/app/Services/FcmService.php +++ b/src/app/Services/FcmService.php @@ -4,6 +4,7 @@ namespace App\Services; use Google\Auth\Credentials\ServiceAccountCredentials; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; class FcmService { @@ -14,20 +15,41 @@ class FcmService public function __construct() { $this->projectid = env('FIREBASE_PROJECT_ID'); - $this->credentials = json_decode( - file_get_contents(base_path(env('FIREBASE_CREDENTIALS'))), - true - ); + + $credentialsPath = base_path(env('FIREBASE_CREDENTIALS')); + + if (!$credentialsPath || !file_exists($credentialsPath)) { + Log::error('Firebase credentials file not found', [ + 'path' => $credentialsPath ?: '(empty)', + ]); + $this->credentials = null; + return; + } + + try { + $this->credentials = json_decode( + file_get_contents($credentialsPath), + true, + 512, + JSON_THROW_ON_ERROR + ); + } catch (\JsonException $e) { + Log::error('Firebase credentials JSON parse error', [ + 'error' => $e->getMessage(), + ]); + $this->credentials = null; + } } public function sendToTopic(string $topic ,string $title,string $body) { + if (!$this->credentials) { + Log::error('Cannot send FCM notification: credentials not loaded'); + return 500; + } + $scopes = ['https://www.googleapis.com/auth/firebase.messaging']; - // $accessToken = ApplicationDefaultCredentials::getAccessToken( - // $scopes, - // $this->credentials - // ); $creds = new ServiceAccountCredentials($scopes, $this->credentials); $tokenArray = $creds->fetchAuthToken(); $accessToken = $tokenArray['access_token'] ?? null;