|
| 1 | +package gg.agit.konect.domain.notification.service; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 7 | +import org.springframework.http.HttpEntity; |
| 8 | +import org.springframework.http.HttpHeaders; |
| 9 | +import org.springframework.http.HttpMethod; |
| 10 | +import org.springframework.http.MediaType; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.retry.annotation.Recover; |
| 13 | +import org.springframework.retry.annotation.Retryable; |
| 14 | +import org.springframework.stereotype.Component; |
| 15 | +import org.springframework.web.client.HttpStatusCodeException; |
| 16 | +import org.springframework.web.client.ResourceAccessException; |
| 17 | +import org.springframework.web.client.RestClientException; |
| 18 | +import org.springframework.web.client.RestTemplate; |
| 19 | + |
| 20 | +import lombok.extern.slf4j.Slf4j; |
| 21 | + |
| 22 | +@Slf4j |
| 23 | +@Component |
| 24 | +public class ExpoPushClient { |
| 25 | + |
| 26 | + private static final String EXPO_PUSH_URL = "https://exp.host/--/api/v2/push/send"; |
| 27 | + private static final String DEFAULT_NOTIFICATION_CHANNEL_ID = "default_notifications"; |
| 28 | + |
| 29 | + private final RestTemplate expoRestTemplate; |
| 30 | + |
| 31 | + public ExpoPushClient(@Qualifier("expoRestTemplate") RestTemplate expoRestTemplate) { |
| 32 | + this.expoRestTemplate = expoRestTemplate; |
| 33 | + } |
| 34 | + |
| 35 | + @Retryable(maxAttempts = 2) |
| 36 | + public void sendNotification(Integer receiverId, List<String> tokens, String title, String body, |
| 37 | + Map<String, Object> data) { |
| 38 | + List<ExpoPushMessage> messages = tokens.stream() |
| 39 | + .map(token -> new ExpoPushMessage(token, title, body, data, DEFAULT_NOTIFICATION_CHANNEL_ID)) |
| 40 | + .toList(); |
| 41 | + |
| 42 | + HttpHeaders headers = new HttpHeaders(); |
| 43 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 44 | + headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
| 45 | + |
| 46 | + HttpEntity<List<ExpoPushMessage>> entity = new HttpEntity<>(messages, headers); |
| 47 | + ResponseEntity<ExpoPushResponse> response = expoRestTemplate.exchange( |
| 48 | + EXPO_PUSH_URL, |
| 49 | + HttpMethod.POST, |
| 50 | + entity, |
| 51 | + ExpoPushResponse.class |
| 52 | + ); |
| 53 | + |
| 54 | + if (!response.getStatusCode().is2xxSuccessful()) { |
| 55 | + throw new IllegalStateException( |
| 56 | + "Expo push response not successful: receiverId=%d, status=%s" |
| 57 | + .formatted(receiverId, response.getStatusCode()) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + ExpoPushResponse responseBody = response.getBody(); |
| 62 | + if (responseBody == null || responseBody.data() == null) { |
| 63 | + throw new IllegalStateException( |
| 64 | + "Expo push response body missing: receiverId=%d".formatted(receiverId) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + for (int i = 0; i < responseBody.data().size(); i += 1) { |
| 69 | + ExpoPushTicket ticket = responseBody.data().get(i); |
| 70 | + if (ticket == null || "ok".equalsIgnoreCase(ticket.status())) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + String token = i < tokens.size() ? tokens.get(i) : "unknown"; |
| 74 | + log.error( |
| 75 | + "Expo 푸시 발송 실패: receiverId={}, token={}, status={}, message={}, details={}", |
| 76 | + receiverId, |
| 77 | + token, |
| 78 | + ticket.status(), |
| 79 | + ticket.message(), |
| 80 | + ticket.details() |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + log.debug("알림 발송 완료: receiverId={}, tokenCount={}", receiverId, tokens.size()); |
| 85 | + } |
| 86 | + |
| 87 | + @Recover |
| 88 | + public void sendNotificationRecover(HttpStatusCodeException e, Integer receiverId, List<String> tokens, |
| 89 | + String title, |
| 90 | + String body, |
| 91 | + Map<String, Object> data) { |
| 92 | + log.error( |
| 93 | + "알림 재시도 후에도 HTTP 오류로 발송에 실패했습니다: receiverId={}, tokenCount={}, statusCode={}, responseBody={}", |
| 94 | + receiverId, |
| 95 | + tokens.size(), |
| 96 | + e.getStatusCode(), |
| 97 | + e.getResponseBodyAsString(), |
| 98 | + e |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + @Recover |
| 103 | + public void sendNotificationRecover(ResourceAccessException e, Integer receiverId, List<String> tokens, |
| 104 | + String title, |
| 105 | + String body, |
| 106 | + Map<String, Object> data) { |
| 107 | + Throwable rootCause = e.getMostSpecificCause(); |
| 108 | + log.error( |
| 109 | + "알림 재시도 후에도 연결 문제로 발송에 실패했습니다: receiverId={}, tokenCount={}, rootCauseType={}, rootCauseMessage={}", |
| 110 | + receiverId, |
| 111 | + tokens.size(), |
| 112 | + rootCause.getClass().getSimpleName(), |
| 113 | + rootCause.getMessage(), |
| 114 | + e |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + @Recover |
| 119 | + public void sendNotificationRecover(IllegalStateException e, Integer receiverId, List<String> tokens, String title, |
| 120 | + String body, |
| 121 | + Map<String, Object> data) { |
| 122 | + log.error( |
| 123 | + "알림 재시도 후에도 Expo 응답이 비정상이라 발송에 실패했습니다: receiverId={}, tokenCount={}, message={}", |
| 124 | + receiverId, |
| 125 | + tokens.size(), |
| 126 | + e.getMessage(), |
| 127 | + e |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + @Recover |
| 132 | + public void sendNotificationRecover(RestClientException e, Integer receiverId, List<String> tokens, String title, |
| 133 | + String body, |
| 134 | + Map<String, Object> data) { |
| 135 | + log.error( |
| 136 | + "알림 재시도 후에도 Rest 클라이언트 오류로 발송에 실패했습니다: receiverId={}, tokenCount={}, exceptionType={}, message={}", |
| 137 | + receiverId, |
| 138 | + tokens.size(), |
| 139 | + e.getClass().getSimpleName(), |
| 140 | + e.getMessage(), |
| 141 | + e |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + @Recover |
| 146 | + public void sendNotificationRecover(Exception e, Integer receiverId, List<String> tokens, String title, String body, |
| 147 | + Map<String, Object> data) { |
| 148 | + log.error( |
| 149 | + "알림 재시도 후에도 예기치 못한 오류로 발송에 실패했습니다: receiverId={}, tokenCount={}, exceptionType={}, message={}", |
| 150 | + receiverId, |
| 151 | + tokens.size(), |
| 152 | + e.getClass().getSimpleName(), |
| 153 | + e.getMessage(), |
| 154 | + e |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + private record ExpoPushMessage(String to, String title, String body, Map<String, Object> data, String channelId) { |
| 159 | + } |
| 160 | + |
| 161 | + private record ExpoPushResponse(List<ExpoPushTicket> data) { |
| 162 | + } |
| 163 | + |
| 164 | + private record ExpoPushTicket(String status, String message, Map<String, Object> details) { |
| 165 | + } |
| 166 | +} |
0 commit comments