From a44125c83d945d792a1e284815c7593ea815f831 Mon Sep 17 00:00:00 2001 From: Hong0329 Date: Mon, 27 May 2024 17:20:28 +0900 Subject: [PATCH] =?UTF-8?q?[FIX]FCM=EA=B4=80=EB=A0=A8=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EC=9A=A9=20=EC=BD=94=EB=93=9C=EB=93=A4=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ContentCommandService.java | 42 +++++++++---------- .../external/fcm/service/FcmService.java | 30 +------------ 2 files changed, 23 insertions(+), 49 deletions(-) diff --git a/DontBeServer/src/main/java/com/dontbe/www/DontBeServer/api/content/service/ContentCommandService.java b/DontBeServer/src/main/java/com/dontbe/www/DontBeServer/api/content/service/ContentCommandService.java index 706884f..955deaf 100644 --- a/DontBeServer/src/main/java/com/dontbe/www/DontBeServer/api/content/service/ContentCommandService.java +++ b/DontBeServer/src/main/java/com/dontbe/www/DontBeServer/api/content/service/ContentCommandService.java @@ -113,29 +113,29 @@ public void likeContent(Long memberId, Long contentId, ContentLikedRequestDto co .notificationText("") .build(); Notification savedNotification = notificationRepository.save(notification); - } - - if (Boolean.TRUE.equals(targetMember.getIsPushAlarmAllowed())) { - String FcmMessageTitle = triggerMember.getNickname() + "님이 " + targetMember.getNickname() + "님의 글을 좋아합니다."; - - FcmMessageDto contentLikeFcmMessage = FcmMessageDto.builder() - .validateOnly(false) - .message(FcmMessageDto.Message.builder() - .notificationDetails(FcmMessageDto.NotificationDetails.builder() - .title(FcmMessageTitle) - .body("") - .build()) - .token(targetMember.getFcmToken()) - .data(FcmMessageDto.Data.builder() - .name("contentLike") - .description("게시글 좋아요 푸시 알림") - .relateContentId(String.valueOf(contentId)) - .build()) - .build()) - .build(); - fcmService.sendMessage(contentLikeFcmMessage); + if (Boolean.TRUE.equals(targetMember.getIsPushAlarmAllowed())) { + String FcmMessageTitle = triggerMember.getNickname() + "님이 " + targetMember.getNickname() + "님의 글을 좋아합니다."; + + FcmMessageDto contentLikeFcmMessage = FcmMessageDto.builder() + .validateOnly(false) + .message(FcmMessageDto.Message.builder() + .notificationDetails(FcmMessageDto.NotificationDetails.builder() + .title(FcmMessageTitle) + .body("") + .build()) + .token(targetMember.getFcmToken()) + .data(FcmMessageDto.Data.builder() + .name("contentLike") + .description("게시글 좋아요 푸시 알림") + .relateContentId(String.valueOf(contentId)) + .build()) + .build()) + .build(); + + fcmService.sendMessage(contentLikeFcmMessage); + } } } diff --git a/DontBeServer/src/main/java/com/dontbe/www/DontBeServer/external/fcm/service/FcmService.java b/DontBeServer/src/main/java/com/dontbe/www/DontBeServer/external/fcm/service/FcmService.java index 3e3dde2..d0c50ae 100644 --- a/DontBeServer/src/main/java/com/dontbe/www/DontBeServer/external/fcm/service/FcmService.java +++ b/DontBeServer/src/main/java/com/dontbe/www/DontBeServer/external/fcm/service/FcmService.java @@ -14,10 +14,7 @@ import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; -import java.io.ByteArrayInputStream; -import java.io.FileInputStream; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.util.Map; @Service @@ -27,34 +24,10 @@ public class FcmService { private final ObjectMapper objectMapper; @PostConstruct -// public void initialize() throws IOException { -// try { -// ClassPathResource resource = new ClassPathResource("fire-base.json"); -// -// String jsonString = new String(resource.getInputStream().readAllBytes(), StandardCharsets.UTF_8); -// System.out.println("Firebase JSON content: " + jsonString); -// -// GoogleCredentials credentials = GoogleCredentials.fromStream(resource.getInputStream()); -// FirebaseOptions options = FirebaseOptions.builder() -// .setCredentials(credentials) -// .build(); -// if (FirebaseApp.getApps().isEmpty()) { -// FirebaseApp.initializeApp(options); -// } -// } catch (IOException e) { -// System.out.println(e.getMessage()); -// } -// } public void initialize() throws IOException { try { ClassPathResource resource = new ClassPathResource("fire-base.json"); - - String jsonString = new String(resource.getInputStream().readAllBytes(), StandardCharsets.UTF_8); - System.out.println("Firebase JSON content: " + jsonString); - - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)); - - GoogleCredentials credentials = GoogleCredentials.fromStream(byteArrayInputStream); + GoogleCredentials credentials = GoogleCredentials.fromStream(resource.getInputStream()); FirebaseOptions options = FirebaseOptions.builder() .setCredentials(credentials) .build(); @@ -66,6 +39,7 @@ public void initialize() throws IOException { } } + public void sendMessage(FcmMessageDto fcmMessageDto) { Message message = Message.builder() .setToken(fcmMessageDto.getMessage().getToken())