Skip to content

Commit

Permalink
Type: FirebaseMessaging 오류 수정 (#104)
Browse files Browse the repository at this point in the history
[Fix] FCM messaging - async 코드 수정 및 에러 처리 추가
  • Loading branch information
pingowl authored Dec 19, 2023
2 parents 38e4e8f + 65dd526 commit 3154599
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/main/java/igoMoney/BE/common/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public enum ErrorCode {
AUTH_CODE_INVALID(HttpStatus.UNAUTHORIZED, "Authorization Code가 유효하지 않습니다."),
FCM_TOKEN_INVALID(HttpStatus.UNAUTHORIZED, "FCM토큰이 null입니다."),

// FCM Messaging 예외
FCM_MESSAGE_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "FCM 메세징 오류"),

// login 예외
LOGIN_CONNECTION_ERROR(HttpStatus.BAD_REQUEST, "로그인 요청 오류"),

Expand Down
40 changes: 40 additions & 0 deletions src/main/java/igoMoney/BE/controller/FCMMessageController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package igoMoney.BE.controller;

import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import igoMoney.BE.service.FCMTokenService;
import igoMoney.BE.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("fcm")
public class FCMMessageController {

private final FCMTokenService fcmTokenService;
private final UserService userService;

@PostMapping("")
public ResponseEntity<Void> sendPushAlarm(@RequestParam(value="fcmToken") String fcmToken,
@RequestParam(value="title") String title,
@RequestParam(value="content") String content){

Message message = Message.builder()
.setNotification(Notification.builder()
.setTitle(title)
.setBody(content)
.build())
.setToken(fcmToken)
.build();
fcmTokenService.sendMessage(message);
return new ResponseEntity(HttpStatus.OK);
}
}
32 changes: 23 additions & 9 deletions src/main/java/igoMoney/BE/service/FCMTokenService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import igoMoney.BE.domain.Notification;
import com.google.firebase.messaging.Notification;
import igoMoney.BE.common.exception.CustomException;
import igoMoney.BE.common.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.concurrent.ExecutionException;

@Repository
@RequiredArgsConstructor
public class FCMTokenService {

private final StringRedisTemplate tokenRedisTemplate;

public void sendNotification(Notification notification) {
Long userId = notification.getUser().getId();
if(!hasKey(userId)){ return;}
public void sendNotification(String title, String content, Long userId) {
if(!hasKey(userId)){
throw new CustomException(ErrorCode.FCM_TOKEN_INVALID);
}
String token = getToken(userId);
Notification fcmNotification = Notification.builder()
.setTitle(title)
.setBody(content)
.build();

Message message = Message.builder()
.putData("title", notification.getTitle())
.putData("content", notification.getMessage())
.putData("userId", String.valueOf(userId))
.setToken(token)
.setNotification(fcmNotification)
.putData("userId", String.valueOf(userId))
.build();
sendMessage(message);
}
Expand All @@ -43,7 +52,12 @@ public boolean hasKey(Long userId){
return tokenRedisTemplate.hasKey(String.valueOf(userId));
}

private void sendMessage(Message message) {
FirebaseMessaging.getInstance().sendAsync(message);
public void sendMessage(Message message) {
try {
FirebaseMessaging.getInstance().sendAsync(message).get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
throw new CustomException(ErrorCode.FCM_MESSAGE_FAILED);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/igoMoney/BE/service/NotificationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class NotificationService {

public void makeNotification(Notification notification){
notificationRepository.save(notification);
fcmTokenService.sendNotification(notification);
fcmTokenService.sendNotification(notification.getTitle(), notification.getMessage() , notification.getUser().getId());
}

}

0 comments on commit 3154599

Please sign in to comment.