카테고리1프로그래밍
카테고리2리액트 네이티브
제목리액트 네이티브 알림받기
작성자고성훈
작성일2025-08-29 12:46:23
1. 패키지 설치
npm install --save @react-native-firebase/app @react-native-firebase/messaging
npm install react-native-push-notification

2. android/build.gradle 설정
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.15'
}
}

3. android/app/build.gradle
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation platform('com.google.firebase:firebase-bom:33.2.0')
implementation 'com.google.firebase:firebase-messaging'
}

4. google-services.json → android/app/ 경로로 복사

5. 권한설정
import messaging from '@react-native-firebase/messaging';

async function requestUserPermission() {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;

if (enabled) {
console.log('Authorization status:', authStatus);
}
}


6. 토큰 가져오기
const fcmToken = await messaging().getToken();
console.log('FCM Token:', fcmToken);


7. 알림 설정
import PushNotification from 'react-native-push-notification';

// 포그라운드
messaging().onMessage(async remoteMessage => {
console.log('Foreground:', remoteMessage);
});

// 백그라운드
messaging().setBackgroundMessageHandler(async remoteMessage => {
PushNotification.localNotification({
channelId: "default-channel-id",
title: remoteMessage.data.title || "새 알림",
message: remoteMessage.data.body || "내용 없음",
});
});

// 알림 열었을 때
messaging().onNotificationOpenedApp(remoteMessage => {
console.log('remoteMessage : ', remoteMessage);
});

// 앱 완전 종료 후 열었을 때
messaging().getInitialNotification().then(remoteMessage => {
if (remoteMessage) {
PushNotification.localNotification({
channelId: "default-channel-id",
title: remoteMessage.data.title || "새 알림",
message: remoteMessage.data.body || "내용 없음",
});
}
});




수정목록