Firebase Cloud Messaging(FCM)은 무료로 메시지를 안정적으로 전송할 수 있는 크로스 플랫폼 메시징 솔루션이다. 쉽게 이야기하면 서버에서 클라이언트로 메시지를 전달할 수 있는 서비스라고 생각하면 된다.
💡 교차 플랫폼(Cross-Platform)
- 하나의 코드베이스로 여러 운영체제(OS)에서 실행될 수 있도록 개발하는 방식
- 즉, 한 번만 개발하면 여러 환경(Android, iOS, Windows, macOS 등)에서 실행 가능하도록 만드는 기술
FCM 구현에는 송수신을 위한 몇가지 구성요소가 있다.
FCM의 동작 과정는 다음과 같다.
FCM에서 메시지를 수신하도록 기기를 등록
클라이언트 앱에 메시지 전송

Spring boot로 FCM을 통해 푸시 알림을 전달하는 방법은 다음과 같다.
먼저 파이어베이스에 로그인하고 콘솔로 이동하여 프로젝트를 생성한다.

json 파일을 다운받는다.
다운 받은 json 파일을 resources/firebase 디렉토리 아래에 service-account.json 이라는 이름으로 저장한다.

build.gradle 파일에 firebase 의존성을 추가해준다.
dependencies {
implementation 'com.google.firebase:firebase-admin:9.2.0'
}
Spring Boot에서 firebase를 사용하기 위해서는 환경 설정이 필요하다.
먼저 나는 application.yaml에 비공개 키의 경로를 속성으로 추가하여 사용하였다.
firebase:
service-account:
path: "/firebase/service-account.json"
이후 FirebaseConfig라는 환경 설정 파일을 만들고 위 경로를 통해 비공개 키를 불러와 Firebase를 초기화하는 코드를 작성하였다.
// FirebaseConfig.java
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
@Slf4j
@Configuration
public class FirebaseConfig {
@Value("${firebase.service-account.path}")
private String SERVICE_ACCOUNT_PATH;
@Bean
public FirebaseApp firebaseApp() {
try {
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(
GoogleCredentials.fromStream(new ClassPathResource(SERVICE_ACCOUNT_PATH).getInputStream())
)
.build();
log.info("Successfully initialized firebase app");
return FirebaseApp.initializeApp(options);
} catch (IOException exception) {
log.error("Fail to initialize firebase app{}", exception.getMessage());
return null;
}
}
@Bean
public FirebaseMessaging firebaseMessaging(FirebaseApp firebaseApp) {
return FirebaseMessaging.getInstance(firebaseApp);
}
}
FcmService라는 서비스 클래스를 만들고 sendNotification() 메서드로 특정 FCM토큰을 가진 유저에게 지정된 title과 body를 포함하여 푸시 알림을 전송하도록 구현하였다.
// FcmService.java
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
@Slf4j
public class FcmService {
private final FirebaseMessaging firebaseMessaging;
public void sendNotification(String title, String body, String fcmToken) {
log.info("Attempting to send Notification (title: {}, body: {}, fcmToken: {})", title, body, fcmToken);
send(createMessage(title, body, fcmToken));
}
private void send(Message message) {
try {
String response = firebaseMessaging.send(message);
log.info("Successfully send Notification: {}", response);
} catch (FirebaseMessagingException e) {
log.error("Fail to send Notification : {}", e.getMessage());
}
}
private Message createMessage(String title, String body, String fcmToken) {
return Message.builder()
.putData("title", title)
.putData("body", body)
.setToken(fcmToken)
.build();
}
}

지금까지 간단하게 Spring boot에서 Firebase를 설정하고 간단하게 알림을 보내는 코드를 작성해보았다.
여기서 더 나아가 회원가입 시 유저의 FCM 토큰을 등록하고 특정 이벤트 발생하면 특정 유저의 FCM 토큰을 조회하여 알림을 보내는 로직을 구상하는 듯 해당 코드를 무궁무진하게 활용할 수 있다.