- FCM 토큰 관리 페이지 (목록, 활성화/비활성화, 삭제) - 테스트 발송 페이지 (대상 필터, 미리보기, 발송) - 발송 이력 페이지 (필터링, 결과 확인) - FcmSender 서비스 (HTTP v1, 배치 처리) - fcm_send_logs 테이블 마이그레이션 - google/auth 패키지 추가
87 lines
2.0 KiB
PHP
87 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Fcm;
|
|
|
|
class FcmResponse
|
|
{
|
|
public function __construct(
|
|
public readonly bool $success,
|
|
public readonly ?string $messageId,
|
|
public readonly ?string $error,
|
|
public readonly int $statusCode,
|
|
public readonly array $rawResponse
|
|
) {}
|
|
|
|
/**
|
|
* 성공 응답 생성
|
|
*/
|
|
public static function success(?string $messageId, int $statusCode, array $rawResponse): self
|
|
{
|
|
return new self(
|
|
success: true,
|
|
messageId: $messageId,
|
|
error: null,
|
|
statusCode: $statusCode,
|
|
rawResponse: $rawResponse
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 실패 응답 생성
|
|
*/
|
|
public static function failure(string $error, int $statusCode, array $rawResponse): self
|
|
{
|
|
return new self(
|
|
success: false,
|
|
messageId: null,
|
|
error: $error,
|
|
statusCode: $statusCode,
|
|
rawResponse: $rawResponse
|
|
);
|
|
}
|
|
|
|
/**
|
|
* FCM 에러 코드 추출
|
|
*/
|
|
public function getErrorCode(): ?string
|
|
{
|
|
if ($this->success) {
|
|
return null;
|
|
}
|
|
|
|
return $this->rawResponse['error']['details'][0]['errorCode'] ?? null;
|
|
}
|
|
|
|
/**
|
|
* 토큰이 유효하지 않은지 확인 (삭제 필요)
|
|
*/
|
|
public function isInvalidToken(): bool
|
|
{
|
|
if ($this->success) {
|
|
return false;
|
|
}
|
|
|
|
// FCM에서 반환하는 무효 토큰 에러 코드들
|
|
$invalidTokenErrors = [
|
|
'UNREGISTERED',
|
|
'INVALID_ARGUMENT',
|
|
'NOT_FOUND',
|
|
];
|
|
|
|
return in_array($this->getErrorCode(), $invalidTokenErrors, true);
|
|
}
|
|
|
|
/**
|
|
* 배열로 변환
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'success' => $this->success,
|
|
'message_id' => $this->messageId,
|
|
'error' => $this->error,
|
|
'status_code' => $this->statusCode,
|
|
];
|
|
}
|
|
}
|