Skip to content

Commit

Permalink
Merge pull request #400 from twenty-three-23/feature/TT-486
Browse files Browse the repository at this point in the history
TT-486
  • Loading branch information
ch8930 authored Nov 12, 2024
2 parents 10ea2b7 + 535abf9 commit ef9c11f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
public interface NotificationService {
void pushNotification(Long userId);
void saveOrUpdateToken(RequestFCMTokenDTO fcmTokenDTO, Long userId);
void testPushNotification(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import com.twentythree.peech.fcm.dto.request.RequestFCMTokenDTO;
import com.twentythree.peech.fcm.entity.NotificationEntity;
import com.twentythree.peech.fcm.event.FCMPushedEvent;
import com.twentythree.peech.fcm.event.FCMTestPushEvent;
import com.twentythree.peech.fcm.infra.NotificationRepository;
import com.twentythree.peech.user.entity.UserEntity;
import com.twentythree.peech.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -53,6 +53,17 @@ public void saveOrUpdateToken(RequestFCMTokenDTO request, Long userId) {
});
}

@Override
public void testPushNotification(Long userId) {
List<String> fcmTokenList = notificationRepository.findAllByUserId(userId);

if(fcmTokenList.isEmpty()){
throw new IllegalStateException("FCM 토큰이 존재하지 않습니다.");
}

applicationEventPublisher.publishEvent(new FCMTestPushEvent(fcmTokenList));
}

private void updateFCMToken(NotificationEntity originEntity, String newToken){
originEntity.updateToken(newToken);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.twentythree.peech.fcm.event;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@Getter
@AllArgsConstructor
public class FCMTestPushEvent {
private List<String> fcmTokenList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.google.firebase.ErrorCode;
import com.google.firebase.messaging.*;
import com.twentythree.peech.fcm.application.NotificationService;
import com.twentythree.peech.fcm.event.FCMPushedEvent;
import com.twentythree.peech.fcm.event.FCMTestPushEvent;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -42,4 +42,25 @@ public void sendMessage(FCMPushedEvent fcmEventDTO) throws FirebaseMessagingExce

}

@EventListener
public void testPush(FCMTestPushEvent fcmTokenList) throws FirebaseMessagingException {

MulticastMessage message = MulticastMessage.builder()
.putData("title", "테스트 푸시 알림")
.putData("body", "테스트 푸시 알림을 보냅니다.")
.addAllTokens(fcmTokenList.getFcmTokenList())
.build();

BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message);
log.info("test messsage 전송완료");

//Todo: 실패한 푸시 알림에 대한 처리
response.getResponses().stream()
.filter(res -> !res.isSuccessful())
.filter(res -> res.getException().getErrorCode() == ErrorCode.INVALID_ARGUMENT)
.forEach(res -> {
log.error("Failed to send message to: id = {} {}", res.getMessageId(), res.getException().getMessage());
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -25,4 +26,12 @@ public ResponseEntity<Void> putFCMToken(RequestFCMTokenDTO request, @Authenticat

return ResponseEntity.ok().build();
}

@GetMapping("/api/v2/notification/test")
public ResponseEntity<String> testNotification(@AuthenticationPrincipal JWTAuthentication jwtAuthentication){
Long userId = jwtAuthentication.getUserId();
notificationService.testPushNotification(userId);

return ResponseEntity.ok("요청 처리 완료");
}
}

0 comments on commit ef9c11f

Please sign in to comment.