feat: FCM 실서비스 확장 - 대량 발송 및 무효 토큰 관리

- FcmSender.sendToMany() 추가 (chunk/rate limit 지원)
- FcmBatchResult 클래스 추가 (발송 결과 집계)
- fcm:send 명령어 추가 (대량 발송, dry-run 지원)
- fcm:prune-invalid 명령어 추가 (무효 토큰 정리)
- PushDeviceToken에 last_error, last_error_at 컬럼 추가
- 실패 토큰 자동 비활성화 (UNREGISTERED, NOT_FOUND, INVALID_ARGUMENT)
This commit is contained in:
2025-12-18 23:01:06 +09:00
parent 81a6dfab5a
commit 10a64fb0a5
9 changed files with 509 additions and 8 deletions

View File

@@ -0,0 +1,105 @@
<?php
namespace App\Console\Commands;
use App\Models\PushDeviceToken;
use Illuminate\Console\Command;
class FcmPruneInvalidCommand extends Command
{
protected $signature = 'fcm:prune-invalid
{--days=30 : N일 이상 된 무효 토큰 삭제}
{--inactive : 비활성(is_active=0) 토큰도 삭제}
{--dry-run : 실제 삭제 없이 대상 수만 출력}
{--force : 확인 없이 강제 실행}';
protected $description = 'FCM 무효 토큰 정리 (soft delete)';
public function handle(): int
{
$days = (int) $this->option('days');
$includeInactive = $this->option('inactive');
$dryRun = $this->option('dry-run');
$this->info('FCM 무효 토큰 정리');
$this->line('');
// 1. 에러가 있는 토큰 (N일 이상)
$errorQuery = PushDeviceToken::withoutGlobalScopes()
->hasError()
->where('last_error_at', '<', now()->subDays($days));
$errorCount = $errorQuery->count();
// 2. 비활성 토큰 (옵션)
$inactiveCount = 0;
if ($includeInactive) {
$inactiveQuery = PushDeviceToken::withoutGlobalScopes()
->where('is_active', false)
->whereNull('last_error') // 에러 없이 비활성화된 것만
->where('updated_at', '<', now()->subDays($days));
$inactiveCount = $inactiveQuery->count();
}
$totalCount = $errorCount + $inactiveCount;
// 현황 출력
$this->table(['구분', '대상 수'], [
["에러 토큰 ({$days}일 이상)", $errorCount],
['비활성 토큰', $includeInactive ? $inactiveCount : '(미포함)'],
['합계', $totalCount],
]);
$this->line('');
if ($totalCount === 0) {
$this->info('정리할 토큰이 없습니다.');
return self::SUCCESS;
}
// Dry-run 모드
if ($dryRun) {
$this->info("🔍 Dry-run: {$totalCount}개 토큰 삭제 예정");
return self::SUCCESS;
}
// 확인
if (! $this->option('force') && ! $this->confirm("정말 {$totalCount}개 토큰을 삭제하시겠습니까?")) {
$this->info('취소되었습니다.');
return self::SUCCESS;
}
// 삭제 실행
$deletedError = 0;
$deletedInactive = 0;
// 에러 토큰 삭제
if ($errorCount > 0) {
$deletedError = PushDeviceToken::withoutGlobalScopes()
->hasError()
->where('last_error_at', '<', now()->subDays($days))
->delete();
$this->info(" 에러 토큰 삭제: {$deletedError}");
}
// 비활성 토큰 삭제
if ($includeInactive && $inactiveCount > 0) {
$deletedInactive = PushDeviceToken::withoutGlobalScopes()
->where('is_active', false)
->whereNull('last_error')
->where('updated_at', '<', now()->subDays($days))
->delete();
$this->info(" 비활성 토큰 삭제: {$deletedInactive}");
}
$this->line('');
$this->info('✅ 총 '.($deletedError + $deletedInactive).'개 토큰 삭제 완료 (soft delete)');
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,151 @@
<?php
namespace App\Console\Commands;
use App\Models\PushDeviceToken;
use App\Services\Fcm\FcmSender;
use Illuminate\Console\Command;
class FcmSendCommand extends Command
{
protected $signature = 'fcm:send
{--tenant= : 테넌트 ID (미지정 시 전체)}
{--user= : 사용자 ID (미지정 시 전체)}
{--platform= : 플랫폼 (android, ios, web)}
{--channel=push_default : 알림 채널 (push_default, push_urgent)}
{--title= : 알림 제목 (필수)}
{--body= : 알림 내용 (필수)}
{--type= : 알림 타입 (invoice_failed 등)}
{--url= : 클릭 시 이동 URL}
{--sound-key= : 사운드 키 (invoice_failed 등)}
{--dry-run : 실제 발송 없이 대상 수만 출력}
{--limit=1000 : 최대 발송 수}';
protected $description = 'FCM 푸시 알림 대량 발송';
public function handle(): int
{
$title = $this->option('title');
$body = $this->option('body');
if (empty($title) || empty($body)) {
$this->error('--title과 --body는 필수입니다.');
return self::FAILURE;
}
// 대상 토큰 조회
$query = PushDeviceToken::withoutGlobalScopes()->active();
if ($tenantId = $this->option('tenant')) {
$query->forTenant((int) $tenantId);
}
if ($userId = $this->option('user')) {
$query->forUser((int) $userId);
}
if ($platform = $this->option('platform')) {
$query->platform($platform);
}
$limit = (int) $this->option('limit');
$tokens = $query->limit($limit)->pluck('token', 'id')->toArray();
$tokenCount = count($tokens);
if ($tokenCount === 0) {
$this->warn('발송 대상 토큰이 없습니다.');
return self::SUCCESS;
}
// 발송 정보 출력
$this->info('FCM 대량 발송');
$this->line('');
$this->table(['항목', '값'], [
['대상 토큰 수', $tokenCount],
['테넌트', $this->option('tenant') ?: '전체'],
['사용자', $this->option('user') ?: '전체'],
['플랫폼', $this->option('platform') ?: '전체'],
['채널', $this->option('channel')],
['제목', $title],
['내용', mb_substr($body, 0, 30).(mb_strlen($body) > 30 ? '...' : '')],
['타입', $this->option('type') ?: '(없음)'],
['URL', $this->option('url') ?: '(없음)'],
]);
$this->line('');
// Dry-run 모드
if ($this->option('dry-run')) {
$this->info("🔍 Dry-run: {$tokenCount}개 토큰 발송 예정");
return self::SUCCESS;
}
// 확인
if (! $this->confirm("정말 {$tokenCount}개 토큰에 발송하시겠습니까?")) {
$this->info('취소되었습니다.');
return self::SUCCESS;
}
// payload 데이터 구성
$data = array_filter([
'type' => $this->option('type'),
'url' => $this->option('url'),
'sound_key' => $this->option('sound-key'),
]);
// 발송 실행
$this->info('발송 중...');
$bar = $this->output->createProgressBar($tokenCount);
$bar->start();
$sender = new FcmSender;
$channel = $this->option('channel');
$tokenValues = array_values($tokens);
$tokenIds = array_keys($tokens);
$result = $sender->sendToMany($tokenValues, $title, $body, $channel, $data);
$bar->finish();
$this->line('');
$this->line('');
// 무효 토큰 처리
$invalidTokens = $result->getInvalidTokens();
if (count($invalidTokens) > 0) {
$this->info('무효 토큰 비활성화 중...');
// 토큰 값으로 DB 업데이트
foreach ($invalidTokens as $token) {
$response = $result->getResponse($token);
$errorCode = $response?->getErrorCode();
PushDeviceToken::withoutGlobalScopes()
->where('token', $token)
->update([
'is_active' => false,
'last_error' => $errorCode,
'last_error_at' => now(),
]);
}
$this->warn(' 비활성화된 토큰: '.count($invalidTokens).'개');
}
// 결과 출력
$summary = $result->toSummary();
$this->line('');
$this->info('✅ 발송 완료');
$this->table(['항목', '값'], [
['전체', $summary['total']],
['성공', $summary['success']],
['실패', $summary['failure']],
['무효 토큰', $summary['invalid_tokens']],
['성공률', $summary['success_rate'].'%'],
]);
return self::SUCCESS;
}
}