Skip to content

Commit

Permalink
Merge pull request #103 from Begin-Vegan/develop
Browse files Browse the repository at this point in the history
[DEPLOY] FCM ์ €์žฅ ์—ฌ๋ถ€ ์ถ”๊ฐ€ ๊ตฌํ˜„
  • Loading branch information
jisujeong0 authored Aug 5, 2024
2 parents 817dc66 + c61272e commit e30af43
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
36 changes: 19 additions & 17 deletions src/main/java/com/beginvegan/domain/fcm/application/FcmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,15 @@ public class FcmService {

@Transactional
public ResponseEntity<?> sendMessageTo(FcmSendDto fcmSendDto) throws IOException {
User user = validateUserById(fcmSendDto.getUserId());
String msg = "๋ฉ”์„ธ์ง€ ์ „์†ก์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค(FCM ํ† ํฐ์ด ์กด์žฌํ•˜์ง€ ์•Š์Œ)";

if (fcmSendDto.getToken() != null) {
Optional<User> findUser = userRepository.findByFcmToken(fcmSendDto.getToken());
DefaultAssert.isTrue(findUser.isPresent(), "์œ ์ € ์ •๋ณด๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
User user = findUser.get();

String fcmToken = user.getFcmToken();
if (fcmToken != null) {
if (user.getAlarmSetting()) {
sendCombinedMessage(fcmSendDto);
sendCombinedMessage(fcmToken, fcmSendDto);
} else {
sendDataMessage(fcmSendDto);
sendDataMessage(fcmToken, fcmSendDto);
}

// alarmType์ด ์กด์žฌํ•  ๊ฒฝ์šฐ์—๋งŒ ์•Œ๋ฆผ ๋‚ด์—ญ์— ์ €์žฅ
Expand All @@ -78,8 +76,8 @@ public ResponseEntity<?> sendMessageTo(FcmSendDto fcmSendDto) throws IOException
return ResponseEntity.ok(apiResponse);
}

private void sendCombinedMessage(FcmSendDto fcmSendDto) throws IOException {
String message = makeFcmMessage(fcmSendDto);
private void sendCombinedMessage(String token, FcmSendDto fcmSendDto) throws IOException {
String message = makeFcmMessage(token, fcmSendDto);

OkHttpClient client = new OkHttpClient();
RequestBody requestBody = RequestBody.create(message,
Expand All @@ -96,11 +94,11 @@ private void sendCombinedMessage(FcmSendDto fcmSendDto) throws IOException {
System.out.println(response.body().string());
}

private String makeFcmMessage(FcmSendDto fcmSendDto) throws JsonProcessingException {
private String makeFcmMessage(String token, FcmSendDto fcmSendDto) throws JsonProcessingException {
FcmMessageDto fcmMessage = FcmMessageDto.builder()
.validateOnly(false)
.message(FcmMessageDto.Message.builder()
.token(fcmSendDto.getToken())
.token(token)
.notification(FcmMessageDto.Notification.builder()
.title(fcmSendDto.getTitle())
.body(fcmSendDto.getBody())
Expand All @@ -125,9 +123,9 @@ private Map<String, String> createDataMassage(FcmSendDto fcmSendDto) {
return data;
}

private void sendDataMessage(FcmSendDto fcmSendDto) throws IOException {
private void sendDataMessage(String token, FcmSendDto fcmSendDto) throws IOException {
Map<String, String> data = createDataMassage(fcmSendDto);
String message = makeDataMessage(fcmSendDto.getToken(), data);
String message = makeDataMessage(token, data);

OkHttpClient client = new OkHttpClient();
RequestBody requestBody = RequestBody.create(message,
Expand Down Expand Up @@ -183,7 +181,7 @@ private String getAccessToken() throws IOException {

public FcmSendDto makeFcmSendDto(User user, AlarmType alarmType, Long itemId, String body, MessageType messageType, UserLevel userLevel) {
return FcmSendDto.builder()
.token(user.getFcmToken())
.userId(user.getId())
.alarmType(alarmType)

.itemId(itemId)
Expand All @@ -196,9 +194,7 @@ public FcmSendDto makeFcmSendDto(User user, AlarmType alarmType, Long itemId, St

@Transactional
public void saveAlarmHistory(FcmSendDto fcmSendDto) {
Optional<User> findUser = userRepository.findByFcmToken(fcmSendDto.getToken());
DefaultAssert.isTrue(findUser.isPresent(), "์œ ์ € ์ •๋ณด๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
User user = findUser.get();
User user = validateUserById(fcmSendDto.getUserId());

Alarm alarm = Alarm.builder()
.alarmType(fcmSendDto.getAlarmType())
Expand All @@ -209,4 +205,10 @@ public void saveAlarmHistory(FcmSendDto fcmSendDto) {

alarmRepository.save(alarm);
}

private User validateUserById(Long userId) {
Optional<User> findUser = userRepository.findById(userId);
DefaultAssert.isTrue(findUser.isPresent(), "์œ ์ € ์ •๋ณด๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
return findUser.get();
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/beginvegan/domain/fcm/dto/FcmSendDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class FcmSendDto {

@Schema(type = "String", example = "BJsVBMM0tWkqZY656FX3kDqhkCXfgKBhcPPSPDgaoa0tYTVKYh5Dt7...", description = "์œ ์ €์˜ FCM ํ† ํฐ์ž…๋‹ˆ๋‹ค.")
private String token;
@Schema(type = "Long", example = "1", description = "์œ ์ €์˜ ๊ณ ์œ  id์ž…๋‹ˆ๋‹ค.")
private Long userId;

@Schema(type = "String", example = "๋น„๊ธด, ๋น„๊ฑด", description = "์•Œ๋ฆผ์˜ ์ œ๋ชฉ์ž…๋‹ˆ๋‹ค.")
private String title;
Expand All @@ -40,8 +40,8 @@ public class FcmSendDto {
private UserLevel userLevel;

@Builder
public FcmSendDto(String token, String title, String body, AlarmType alarmType, Long itemId, MessageType messageType, UserLevel userLevel) {
this.token = token;
public FcmSendDto(Long userId, String title, String body, AlarmType alarmType, Long itemId, MessageType messageType, UserLevel userLevel) {
this.userId = userId;
this.title = title;
this.body = body;
this.alarmType = alarmType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ public ResponseEntity<?> updateVeganType(UserPrincipal userPrincipal, UpdateVega
return ResponseEntity.ok(apiResponse);
}

public ResponseEntity<?> getFcmTokenStatus(UserPrincipal userPrincipal) {
User user = validateUserById(userPrincipal.getId());
boolean isStored = user.getFcmToken() != null;

StoredFcmTokenRes storedFcmTokenRes = StoredFcmTokenRes.builder()
.storedFcmToken(isStored)
.build();

ApiResponse apiResponse = ApiResponse.builder()
.check(true)
.information(storedFcmTokenRes)
.build();
return ResponseEntity.ok(apiResponse);
}

public ResponseEntity<?> getAlarmSetting(UserPrincipal userPrincipal) {
User user = validateUserById(userPrincipal.getId());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.beginvegan.domain.user.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;

@Data
public class StoredFcmTokenRes {

@Schema(type = "boolean", example = "true", description = "์œ ์ €์˜ fcm ํ† ํฐ ์ €์žฅ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค.")
private Boolean storedFcmToken;

@Builder
public StoredFcmTokenRes(Boolean storedFcmToken) { this.storedFcmToken = storedFcmToken; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ public ResponseEntity<?> findUserByToken(
return userService.findUserByToken(userPrincipal);
}

@Operation(summary = "์œ ์ €์˜ FCM ํ† ํฐ ์ €์žฅ ์—ฌ๋ถ€ ์กฐํšŒ", description = "์œ ์ €์˜ FCM ํ† ํฐ ์ €์žฅ ์—ฌ๋ถ€๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "FCM ํ† ํฐ ์ €์žฅ ์—ฌ๋ถ€ ์กฐํšŒ ์„ฑ๊ณต", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = StoredFcmTokenRes.class) ) } ),
@ApiResponse(responseCode = "400", description = "FCM ํ† ํฐ ์ €์žฅ ์—ฌ๋ถ€ ์กฐํšŒ ์‹คํŒจ", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ),
})
@GetMapping("/fcm")
public ResponseEntity<?> getFcmTokenStatus(
@Parameter(description = "Accesstoken์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.", required = true) @CurrentUser UserPrincipal userPrincipal
) {
return userService.getFcmTokenStatus(userPrincipal);
}

@Operation(summary = "์œ ์ €์˜ ์•Œ๋ฆผ ์—ฌ๋ถ€ ์กฐํšŒ", description = "์œ ์ €์˜ ์•Œ๋ฆผ ์—ฌ๋ถ€๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "์œ ์ € ์•Œ๋žŒ ์—ฌ๋ถ€ ์กฐํšŒ ์„ฑ๊ณต", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = AlarmSettingRes.class))}),
Expand Down

0 comments on commit e30af43

Please sign in to comment.