fix(reliability): resolve F-09 — add error handling for FCM credential loading
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Services;
|
|||||||
|
|
||||||
use Google\Auth\Credentials\ServiceAccountCredentials;
|
use Google\Auth\Credentials\ServiceAccountCredentials;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
class FcmService
|
class FcmService
|
||||||
{
|
{
|
||||||
@@ -14,20 +15,41 @@ class FcmService
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->projectid = env('FIREBASE_PROJECT_ID');
|
$this->projectid = env('FIREBASE_PROJECT_ID');
|
||||||
$this->credentials = json_decode(
|
|
||||||
file_get_contents(base_path(env('FIREBASE_CREDENTIALS'))),
|
$credentialsPath = base_path(env('FIREBASE_CREDENTIALS'));
|
||||||
true
|
|
||||||
);
|
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)
|
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'];
|
$scopes = ['https://www.googleapis.com/auth/firebase.messaging'];
|
||||||
|
|
||||||
// $accessToken = ApplicationDefaultCredentials::getAccessToken(
|
|
||||||
// $scopes,
|
|
||||||
// $this->credentials
|
|
||||||
// );
|
|
||||||
$creds = new ServiceAccountCredentials($scopes, $this->credentials);
|
$creds = new ServiceAccountCredentials($scopes, $this->credentials);
|
||||||
$tokenArray = $creds->fetchAuthToken();
|
$tokenArray = $creds->fetchAuthToken();
|
||||||
$accessToken = $tokenArray['access_token'] ?? null;
|
$accessToken = $tokenArray['access_token'] ?? null;
|
||||||
|
|||||||
Reference in New Issue
Block a user