-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/#133 fcm서버에 알림 정보 보내기 #161
The head ref may contain hidden characters: "Feature/#133-FCM\uC11C\uBC84\uC5D0_\uC54C\uB9BC_\uC815\uBCF4_\uBCF4\uB0B4\uAE30"
Changes from 6 commits
6cd8f72
e4aee03
3f5229a
ca5218c
f051ce4
25fffbb
0320321
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.emmsale.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class RestTemplateConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import com.emmsale.login.application.dto.GithubProfileResponse; | ||
import com.emmsale.login.exception.LoginException; | ||
import com.emmsale.login.exception.LoginExceptionType; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
|
@@ -15,10 +16,9 @@ | |
import org.springframework.web.client.RestTemplate; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class GithubClient { | ||
|
||
private static final RestTemplate REST_TEMPLATE = new RestTemplate(); | ||
|
||
@Value("${github.client.id}") | ||
private String clientId; | ||
@Value("${github.client.secret}") | ||
|
@@ -28,6 +28,8 @@ public class GithubClient { | |
@Value("${github.url.profile}") | ||
private String profileUrl; | ||
|
||
private final RestTemplate restTemplate; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍👍 |
||
|
||
public String getAccessTokenFromGithub(final String code) { | ||
final GithubAccessTokenRequest githubAccessTokenRequest = buildGithubAccessTokenRequest(code); | ||
|
||
|
@@ -54,7 +56,7 @@ private GithubProfileResponse getGithubProfileResponse(final String accessToken) | |
|
||
final HttpEntity<String> httpEntity = new HttpEntity<>(headers); | ||
|
||
return REST_TEMPLATE | ||
return restTemplate | ||
.exchange(profileUrl, HttpMethod.GET, httpEntity, GithubProfileResponse.class) | ||
.getBody(); | ||
} | ||
|
@@ -76,7 +78,7 @@ private String getAccessTokenResponse(final GithubAccessTokenRequest githubAcces | |
headers | ||
); | ||
|
||
return REST_TEMPLATE | ||
return restTemplate | ||
.exchange(accessTokenUrl, HttpMethod.POST, httpEntity, GithubAccessTokenResponse.class) | ||
.getBody() | ||
.getAccessToken(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.emmsale.notification.application; | ||
|
||
import static com.emmsale.notification.exception.NotificationExceptionType.*; | ||
import static com.emmsale.notification.exception.NotificationExceptionType.CONVERTING_JSON_ERROR; | ||
import static com.emmsale.notification.exception.NotificationExceptionType.NOT_FOUND_FCM_TOKEN; | ||
|
||
import com.emmsale.member.domain.Member; | ||
import com.emmsale.member.domain.MemberRepository; | ||
import com.emmsale.member.exception.MemberException; | ||
import com.emmsale.member.exception.MemberExceptionType; | ||
import com.emmsale.notification.application.dto.FcmMessage; | ||
import com.emmsale.notification.application.dto.FcmMessage.Data; | ||
import com.emmsale.notification.application.dto.FcmMessage.Message; | ||
import com.emmsale.notification.domain.FcmToken; | ||
import com.emmsale.notification.domain.FcmTokenRepository; | ||
import com.emmsale.notification.domain.Notification; | ||
import com.emmsale.notification.exception.NotificationException; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import java.io.IOException; | ||
import java.util.List; | ||
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.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class FirebaseCloudMessageClient { | ||
|
||
private static final String PREFIX_ACCESS_TOKEN = "Bearer "; | ||
private static final String PREFIX_FCM_REQUEST_URL = "https://fcm.googleapis.com/v1/projects/"; | ||
private static final String POSTFIX_FCM_REQUEST_URL = "/messages:send"; | ||
private static final String FIREBASE_KEY_PATH = "firebase-kerdy.json"; | ||
private static final boolean DEFAULT_VALIDATE_ONLY = false; | ||
|
||
private final ObjectMapper objectMapper; | ||
private final MemberRepository memberRepository; | ||
private final RestTemplate restTemplate; | ||
private final FcmTokenRepository fcmTokenRepository; | ||
|
||
@Value("${firebase.project.id}") | ||
private String projectId; | ||
|
||
public void sendMessageTo(final Long receiverId, final Notification notification) { | ||
|
||
final FcmToken fcmToken = fcmTokenRepository.findByMemberId(receiverId) | ||
.orElseThrow(() -> new NotificationException(NOT_FOUND_FCM_TOKEN)); | ||
|
||
final String message = makeMessage(fcmToken.getToken(), notification); | ||
|
||
final HttpHeaders httpHeaders = new HttpHeaders(); | ||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); | ||
httpHeaders.add(HttpHeaders.AUTHORIZATION, PREFIX_ACCESS_TOKEN + getAccessToken()); | ||
|
||
final HttpEntity<String> httpEntity = new HttpEntity<>(message, httpHeaders); | ||
|
||
final String fcmRequestUrl = PREFIX_FCM_REQUEST_URL + projectId + POSTFIX_FCM_REQUEST_URL; | ||
|
||
final ResponseEntity<String> exchange = restTemplate.exchange( | ||
fcmRequestUrl, | ||
HttpMethod.POST, | ||
httpEntity, | ||
String.class | ||
); | ||
|
||
if (exchange.getStatusCode().isError()) { | ||
log.error("firebase 접속 에러 = {}", exchange.getBody()); | ||
} | ||
} | ||
|
||
private String makeMessage(final String targetToken, final Notification notification) { | ||
|
||
final Long senderId = notification.getSenderId(); | ||
final Member sender = memberRepository.findById(senderId) | ||
.orElseThrow(() -> new MemberException(MemberExceptionType.NOT_FOUND_MEMBER)); | ||
|
||
final Data messageData = new Data( | ||
sender.getName(), senderId.toString(), | ||
notification.getReceiverId().toString(), notification.getMessage(), | ||
sender.getOpenProfileUrl() | ||
); | ||
|
||
final Message message = new Message(messageData, targetToken); | ||
|
||
final FcmMessage fcmMessage = new FcmMessage(DEFAULT_VALIDATE_ONLY, message); | ||
|
||
try { | ||
return objectMapper.writeValueAsString(fcmMessage); | ||
} catch (JsonProcessingException e) { | ||
log.error("메세지 보낼 때 JSON 변환 에러", e); | ||
throw new NotificationException(CONVERTING_JSON_ERROR); | ||
} | ||
} | ||
|
||
private String getAccessToken() { | ||
final String firebaseConfigPath = FIREBASE_KEY_PATH; | ||
|
||
try { | ||
final GoogleCredentials googleCredentials = GoogleCredentials | ||
.fromStream(new ClassPathResource(firebaseConfigPath).getInputStream()) | ||
.createScoped(List.of("https://www.googleapis.com/auth/cloud-platform")); | ||
|
||
googleCredentials.refreshIfExpired(); | ||
|
||
return googleCredentials.getAccessToken().getTokenValue(); | ||
} catch (IOException e) { | ||
log.error("구글 토큰 요청 에러", e); | ||
throw new NotificationException(GOOGLE_REQUEST_TOKEN_ERROR); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.emmsale.notification.application.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
public class FcmMessage { | ||
|
||
@JsonProperty("validate_only") | ||
private final boolean validateOnly; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 이게 무엇을 의미하는지 간략하게 알려주실 수 있나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현장 리뷰 완료요~ |
||
private final Message message; | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
public static class Message { | ||
|
||
private final Data data; | ||
private final String token; | ||
} | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
public static class Data { | ||
|
||
private final String senderName; | ||
private final String senderId; | ||
private final String receiverId; | ||
private final String message; | ||
private final String openProfileUrl; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,7 @@ security: | |
token: | ||
secret-key: secret_key | ||
expire-length: 3_600_000_000 | ||
|
||
firebase: | ||
project: | ||
id: kerdy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 이렇게 별도로 분리한 이유가 있을까요?