Skip to content

Commit

Permalink
Merge pull request #123 from wooyeon0626/feature/chatting
Browse files Browse the repository at this point in the history
[FEAT] FCM 메시지 DATA 추가
  • Loading branch information
tenta3802 authored Feb 29, 2024
2 parents 46198cd + e2eec78 commit c47545d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void enter(StompDto stompDto, @Header("Authorization") String token) {
log.info("session count = " + sessionStore.get(roomId.toString()));
log.info("FCM 메시지 전송함");
try {
fcmService.sendMessageTo(FcmDto.buildRequest(loginEmail, stompDto, userRepository, matchRepository));
fcmService.sendMessageTo(FcmDto.buildRequest(loginEmail, stompDto, roomId, userRepository, matchRepository));
} catch (IOException e) {
throw new WooyeonException(ExceptionCode.FCM_SEND_FAIL_ERROR);
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/wooyeon/yeon/common/fcm/dto/FcmDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static class Notification {
public static class Data {
private String name;
private String description;
private int chatRoomId;
private String type;
}

@Getter
Expand All @@ -57,6 +59,7 @@ public static class Request {
String body;
String email;
String description;
int chatRoomId;
}

@Getter
Expand All @@ -72,8 +75,8 @@ public static class SaveResponse {
private HttpStatus status;
}

public static Request buildRequest(String loginEmail , StompDto stompDto, UserRepository userRepository,
MatchRepository matchRepository) {
public static Request buildRequest(String loginEmail , StompDto stompDto, Long roomId,
UserRepository userRepository, MatchRepository matchRepository) {

User user = userRepository.findOptionalByEmail(loginEmail)
.orElseThrow(() -> new IllegalArgumentException(ExceptionCode.LOGIN_USER_NOT_FOUND.toString()));
Expand All @@ -91,11 +94,12 @@ public static Request buildRequest(String loginEmail , StompDto stompDto, UserRe
User matchUser = userRepository.findOptionalByEmail(matchUserId)
.orElseThrow(() -> new IllegalArgumentException(ExceptionCode.LOGIN_USER_NOT_FOUND.toString()));

return FcmDto.Request.builder()
return Request.builder()
.title(user.getUserProfile().getNickname())
.body(stompDto.getMessage())
.targetToken(matchUser.getFcmToken())
.email(loginEmail)
.chatRoomId(Integer.parseInt(String.valueOf(roomId)))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void sendMessageTo(FcmDto.Request request) throws IOException {
request.getTitle(),
request.getBody(),
request.getEmail(),
request.getDescription());
request.getDescription(),
request.getChatRoomId());

OkHttpClient client = new OkHttpClient();
RequestBody requestBody = RequestBody.create(message, MediaType.get("application/json; charset=utf-8"));
Expand Down Expand Up @@ -82,7 +83,7 @@ public void sendMessageTo(FcmDto.Request request) throws IOException {
* @return
*/
public String makeMessage(
String targetToken, String title, String body, String email, String description
String targetToken, String title, String body, String email, String description, int chatRoomId
) throws JsonProcessingException {

FcmDto fcmMessage = FcmDto.builder()
Expand All @@ -97,6 +98,8 @@ public String makeMessage(
)
.data(
FcmDto.Data.builder()
.type("chat")
.chatRoomId(chatRoomId)
.name(email)
.description(description)
.build()
Expand All @@ -105,7 +108,6 @@ public String makeMessage(
)
.validateOnly(false)
.build();

return objectMapper.writeValueAsString(fcmMessage);
}

Expand Down

0 comments on commit c47545d

Please sign in to comment.