-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: fcm sdk 적용 및 fcm 서버측 알림처리 비동기 설정 * refactor: FCM 초기화 로직 변경 * refactor: FCM 파일 경로 변경 * refactor: FCM 인스턴스 명 변경 * test: 알림 테스트 계정 변경 * chore: Async 스레드 풀 개수 변경 * chore: 알림 로깅 추가 및 불필요한 코드 삭제 * refactor: 파일 경로 정적 변수로 추출 * refactor: 비동기 스레드 풀 40으로 설정 * chore: 현재 필요하지 않는 테스트 메서드 주석 처리 * chore: FCM 정적 변수 빈 주입으로 변경 --------- Co-authored-by: 이건회 <[email protected]>
- Loading branch information
1 parent
0f13d5d
commit 04f7066
Showing
5 changed files
with
76 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 40 additions & 57 deletions
97
...d/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,64 @@ | ||
package com.official.pium.notification.fcm.application; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.official.pium.notification.fcm.dto.FcmMessageResponse; | ||
import com.official.pium.notification.fcm.exception.FcmException; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.Message; | ||
import com.google.firebase.messaging.Notification; | ||
import com.official.pium.notification.application.MessageSendManager; | ||
import jakarta.annotation.PostConstruct; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
import java.io.IOException; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class FcmMessageSender implements MessageSendManager { | ||
|
||
@Value("${fcm.api.url}") | ||
private String apiUrl; | ||
|
||
@Value("${fcm.key.path}") | ||
private String keyPath; | ||
|
||
@Value("${fcm.key.scope}") | ||
private String keyScope; | ||
@Value("${fcm.json.path}") | ||
private String FCM_JSON_PATH; | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
public void sendMessageTo(String targetToken, String title, String body) { | ||
@PostConstruct | ||
public void initialize() { | ||
try { | ||
FcmMessageResponse message = makeMessage(targetToken, title, body); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken()); | ||
headers.set(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8"); | ||
|
||
HttpEntity<FcmMessageResponse> request = new HttpEntity<>(message, headers); | ||
ClassPathResource resource = new ClassPathResource(FCM_JSON_PATH); | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(resource.getInputStream())) | ||
.build(); | ||
|
||
ResponseEntity<FcmMessageResponse> postResult = restTemplate.postForEntity( | ||
apiUrl, | ||
request, | ||
FcmMessageResponse.class | ||
); | ||
|
||
log.info("FCM 메시지 전송 성공: {}", postResult.getBody()); | ||
|
||
} catch (Exception e) { | ||
log.error("FCM 메시지 전송 실패", e); | ||
throw new FcmException.FcmMessageSendException(e.getMessage()); | ||
if (FirebaseApp.getApps().isEmpty()) { | ||
FirebaseApp.initializeApp(options); | ||
} | ||
} catch (FileNotFoundException e) { | ||
log.error("파일을 찾을 수 없습니다. ", e); | ||
} catch (IOException e) { | ||
log.error("FCM 인증이 실패했습니다. ", e); | ||
} | ||
} | ||
|
||
private FcmMessageResponse makeMessage(String targetToken, String title, String body) { | ||
return FcmMessageResponse.builder() | ||
.message(FcmMessageResponse.Message.builder() | ||
.token(targetToken) | ||
.notification(FcmMessageResponse.Notification.builder() | ||
.title(title) | ||
.body(body) | ||
.image(null) | ||
.build() | ||
) | ||
.build() | ||
) | ||
.validate_only(false) | ||
public void sendMessageTo(String targetToken, String title, String body) { | ||
Notification notification = Notification.builder() | ||
.setTitle(title) | ||
.setBody(body) | ||
.build(); | ||
} | ||
|
||
private String getAccessToken() throws IOException { | ||
GoogleCredentials googleCredentials = GoogleCredentials | ||
.fromStream(new ClassPathResource(keyPath).getInputStream()) | ||
.createScoped(keyScope); | ||
googleCredentials.refreshIfExpired(); | ||
return googleCredentials.getAccessToken().getTokenValue(); | ||
Message message = Message.builder() | ||
.setToken(targetToken) | ||
.setNotification(notification) | ||
.build(); | ||
try { | ||
String response = FirebaseMessaging.getInstance().sendAsync(message).get(); | ||
log.info("알림 전송 성공 : " + response); | ||
} catch (InterruptedException e) { | ||
log.error("FCM 알림 스레드에서 문제가 발생했습니다.", e); | ||
} catch (ExecutionException e) { | ||
log.error("FCM 알림 전송에 실패했습니다.", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters