114 lines
3.3 KiB
PHP
114 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* FCM 발송 API 서비스
|
|
*
|
|
* MNG에서 API 서버의 FCM 발송 엔드포인트를 호출합니다.
|
|
*/
|
|
class FcmApiService
|
|
{
|
|
private string $baseUrl;
|
|
|
|
private ?string $apiKey;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->baseUrl = rtrim(config('services.api.base_url', ''), '/');
|
|
$this->apiKey = config('services.api.key');
|
|
}
|
|
|
|
/**
|
|
* FCM 푸시 발송
|
|
*
|
|
* @param array $data 발송 데이터
|
|
* @param int|null $senderId 발송자 ID (MNG 관리자 ID)
|
|
* @return array{success: bool, data?: array, message?: string}
|
|
*/
|
|
public function send(array $data, ?int $senderId = null): array
|
|
{
|
|
try {
|
|
$response = Http::timeout(30)
|
|
->withHeaders([
|
|
'X-API-KEY' => $this->apiKey,
|
|
'X-Sender-Id' => $senderId,
|
|
'Accept' => 'application/json',
|
|
])
|
|
->post("{$this->baseUrl}/api/v1/admin/fcm/send", $data);
|
|
|
|
if ($response->successful()) {
|
|
return [
|
|
'success' => true,
|
|
'data' => $response->json('data'),
|
|
'message' => $response->json('message'),
|
|
];
|
|
}
|
|
|
|
Log::warning('[FcmApiService] Send failed', [
|
|
'status' => $response->status(),
|
|
'response' => $response->json(),
|
|
]);
|
|
|
|
return [
|
|
'success' => false,
|
|
'message' => $response->json('message') ?? 'FCM 발송에 실패했습니다.',
|
|
];
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error('[FcmApiService] Send exception', [
|
|
'exception' => $e->getMessage(),
|
|
]);
|
|
|
|
return [
|
|
'success' => false,
|
|
'message' => 'API 서버 연결에 실패했습니다: '.$e->getMessage(),
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 대상 토큰 수 미리보기
|
|
*
|
|
* @param array $filters 필터 조건
|
|
* @return array{success: bool, count?: int, message?: string}
|
|
*/
|
|
public function previewCount(array $filters): array
|
|
{
|
|
try {
|
|
$response = Http::timeout(10)
|
|
->withHeaders([
|
|
'X-API-KEY' => $this->apiKey,
|
|
'Accept' => 'application/json',
|
|
])
|
|
->get("{$this->baseUrl}/api/v1/admin/fcm/preview-count", $filters);
|
|
|
|
if ($response->successful()) {
|
|
return [
|
|
'success' => true,
|
|
'count' => $response->json('data.count', 0),
|
|
];
|
|
}
|
|
|
|
return [
|
|
'success' => false,
|
|
'count' => 0,
|
|
'message' => $response->json('message') ?? '미리보기 조회에 실패했습니다.',
|
|
];
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error('[FcmApiService] Preview count exception', [
|
|
'exception' => $e->getMessage(),
|
|
]);
|
|
|
|
return [
|
|
'success' => false,
|
|
'count' => 0,
|
|
'message' => 'API 서버 연결에 실패했습니다.',
|
|
];
|
|
}
|
|
}
|
|
} |