728x90
- FCM 토큰 추출
- Backend 등록
- Backend 메세지 수신
서비스 키 다운
https://console.firebase.google.com/
로그인 - Google 계정
이메일 또는 휴대전화
accounts.google.com
프로젝트 설정 > 서비스 계정 > 새 비공개키 생성
다운 받은 JSON 을 저장해둔다.

클라이언트 토큰 추출
import messaging, { FirebaseMessagingTypes } from "@react-native-firebase/messaging";
if (permissionGranted) {
try {
// 토큰을 추출해서 백엔드로 넘겨준다.
const token = await messagingInstance.getToken();
if (token) {
// registerFcmTokenToBackend 백엔드로 보내는 코드 구현
await registerFcmTokenToBackend(token);
}
} catch (err) {
console.error("❌ FCM 토큰 처리 실패:", err);
}
} else {
console.warn("❗ 알림 권한이 거부되어 FCM 토큰을 가져올 수 없습니다.");
}
백엔드
// FireBase Admin
implementation("com.google.firebase:firebase-admin:9.3.0")
@Configuration
class FirebaseConfig {
@PostConstruct
fun init() {
try {
val serviceAccount = ClassPathResource("fcm-key.json").inputStream
val options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build()
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
위에서 받은 서비스 키 경로를 넣어준다.
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.Message
import com.google.firebase.messaging.Notification
@Component
@Transactional
class FcmManager(
val customerService: CustomerService
) {
fun sendPushNotification(req: FCMResponseDto) {
val customer = customerService.findModelById(req.targetCustomerId)
val fcmToken = customer.fcmToken ?: return logger.info("FCM token 이 등록되지 않은 사용자 입니다")
val message: Message = Message.builder()
// 클라이언트에서 추출 후 저장 한 토큰
.setToken(fcmToken)
.setNotification(
Notification.builder()
// 메세지 제목
.setTitle("Title")
// 메세지 본문
.setBody(
req.notificationType.formatMessage(
content = req.content, senderNickname = req.senderNickname
)
)
.build()
)
.build()
try {
// 메세지 발신
FirebaseMessaging.getInstance().send(message)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
끗
728x90
'개-발 > Java + Spring + Kotlin' 카테고리의 다른 글
| [Spring] chatGpt api 연동 (0) | 2025.09.18 |
|---|---|
| [Spring] itemReader 에서 ReadOnly DB 읽기 (0) | 2025.09.05 |
| [Spring] storekit2 / expo 인 앱 아이템 구매 확인 구현 3 (AOS) (0) | 2025.08.07 |
| [Spring] storekit2 / expo 인 앱 아이템 구매 확인 구현 2 (IOS) (2) | 2025.08.07 |
| [kotlin] 슬랙 봇 연동하기 (0) | 2025.06.20 |