Skip to content

FCM 알림 적용 방법 - 스프링 코드 리포지터리입니다. (24.07)

Notifications You must be signed in to change notification settings

devholic22/fcm-introduce-spring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fcm-introduce-spring

본 리포지터리는 블로그 내용을 쉽게 실습하실 수 있도록 스프링 환경에서의 FCM 알림 기능 구현 코드를 담은 리포지터리입니다.

적용한 기술 환경

  • Spring Boot 3.3.2
  • Java 17
  • Spring Data JPA 3.3.2
  • Hibernate 6.5.2
  • Spring Data Redis 3.3.2
  • Spring Web 3.3.2
  • Lombok 1.18.34
  • MySQL 8.3.0
  • FCM (firebase-admin) 9.3.0

주의사항

1. 파이어베이스 파일이 필요합니다.

샘플 코드에는 firebase.json 파일을 보관해두지 않았습니다.
블로그 글을 참고하여 파이어베이스 파일을 받은 뒤 src/main/resources/firebase.json으로 저장해주시기 바랍니다.

2. 파이어베이스 토큰이 필요합니다.

샘플 코드에는 파이어베이스 토큰을 아래처럼 작성해두었기 때문에 제대로 실행되지 않습니다.
블로그 글을 참고하여 토큰을 발급받은 뒤 대체하시기 바랍니다.

@RequiredArgsConstructor
@Transactional
@Service
public class AlertService {

    private static final String TOKEN = "..."; // 실제 FCM 토큰으로 테스트 해 보시면 됩니다.

    private final AlertRepository alertRepository;
    private final AlertTokenRepository tokenRepository;
    private final AlertManager alertManager;

    // 토큰 저장
    public void saveToken(final Long id, final String token) {
        tokenRepository.saveToken(id, token);
    }

    // 알림 전송
    public void sendAlert(final String title, final String body, final String sender, final Long receiverId) {
        // String token = tokenRepository.getToken(receiverId); 프로젝트에서는 레디스를 이용해서 토큰을 조회합니다.
        Alert alert = Alert.createWith(title, body, receiverId);
        Alert savedAlert = alertRepository.save(alert);
        alertManager.send(savedAlert, sender, TOKEN);
    }
}

참고 링크 (블로그 글, Pull Request)

About

FCM 알림 적용 방법 - 스프링 코드 리포지터리입니다. (24.07)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages