상식닷컴
로그인
가입하기
2026년 상식닷컴 선정 식당 & 카페 리스트
2025년 2026년 신상 호텔 리스트
최근에 오픈한 호텔을 찾는다면 살펴보세요
일주일 식단표 어플
자동 일주일 식단표 어플
안드로이드
아이폰
주식 & 코인 차트의 신
1000만원으로 2000만원 만들기 프로젝트
수정하기 - 리액트 네이티브에서 푸시 알림을 구현하는 방법은 무엇인가요?
닉네임
비밀번호
제목
내용
[이미지 업로드는 권한이 있는 사람만 가능. 하단 카톡으로 연락]
리액트 네이티브에서 푸시 알림을 구현하는 방법은 여러 단계로 나눌 수 있으며, 주로 Firebase Cloud Messaging(<a href='https://sangseek.com/sangseeks/FCM/ko'>FCM</a>)을 사용하여 구현합니다. 아래는 리액트 네이티브에서 푸시 알림을 설정하고 사용하는 방법에 대한 자세한 설명입니다. 1. 프로젝트 설정 1.1. 리액트 네이티브 프로젝트 생성 먼저, 리액트 네이티브 프로젝트를 생성합니다. 아래 명령어를 사용하여 새로운 프로젝트를 생성할 수 있습니다. ```bash npx <a href='https://sangseek.com/sangseeks/react-native/ko'>react-native</a> init MyPushNotificationApp cd MyPushNotificationApp ``` 1.2. Firebase 설정 1. Firebase Console에 로그인 : [Firebase Console](https://console.firebase.google.com/)에 로그인합니다. 2. 새 프로젝트 생성 : 새 프로젝트를 생성하고, Android 및 iOS 앱을 추가합니다. 3. Firebase Cloud Messaging 활성화 : Firebase 프로젝트의 설정에서 Cloud Messaging을 활성화합니다. 4. google-services.json 및 GoogleService-Info.plist 다운로드 : Android와 iOS 각각에 필요한 설정 파일을 다운로드합니다. 2. 라이브러리 설치 푸시 알림을 구현하기 위해 필요한 라이브러리를 설치합니다. `@react-native-firebase/app`과 `@react-native-firebase/messaging`을 설치합니다. ```bash npm install @react-native-firebase/app @react-native-firebase/messaging ``` 또한, iOS의 경우 CocoaPods를 설치해야 합니다. ```bash cd ios pod install cd .. ``` 3. Android 설정 3.1. <a href='https://sangseek.com/sangseeks/AndroidManifest.xml/ko'>AndroidManifest.xml</a> 수정 `android/app/src/main/AndroidManifest.xml` 파일을 열고, 다음과 같은 권한과 서비스를 추가합니다. ```xml <manifest> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application> ... <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESS<a href='https://sangseek.com/sangseeks/AGI/ko'>AGI</a>NG_EVENT"/> </intent-filter> </service> </application> </manifest> ``` 3.2. Firebase 설정 파일 추가 `android/app` 폴더에 `google-services.json` 파일을 추가합니다. 4. iOS 설정 4.1. Info.plist 수정 `ios/MyPushNotificationApp/Info.plist` 파일을 열고, 다음과 같은 항목을 추가합니다. ```xml <key><a href='https://sangseek.com/sangseeks/UIB/ko'>UIB</a>ackgroundModes</key> <array> <string>fetch</string> <string>remote-notification</string> </array> <key>Firebase<a href='https://sangseek.com/sangseeks/AppDelegate/ko'>AppDelegate</a>ProxyEnabled</key> <false/> ``` 4.2. Firebase 설정 파일 추가 `ios/MyPushNotificationApp` 폴더에 `GoogleService-Info.plist` 파일을 추가합니다. 5. 푸시 알림 수신 및 처리 이제 푸시 알림을 수신하고 처리하는 코드를 작성합니다. `App.js` 파일을 열고 다음과 같이 수정합니다. ```javascript import <a href='https://sangseek.com/sangseeks/React/ko'>React</a>, { useEffect } from 'react'; import { Alert } from 'react-native'; import messaging from '@react-native-firebase/messaging'; const App = () => { useEffect(() => { // 포그라운드에서 알림 수신 const unsubscribe = messaging().onMessage(async remoteMessage => { Alert.alert('새 알림!', remoteMessage.notification.body); }); // 백그라운드에서 알림 수신 messaging().setBackgroundMessageHandler(async remoteMessage => { console.log('백그라운드에서 수신한 메시지:', remoteMessage); }); return unsubscribe; }, []); return ( // Your app UI ); }; export default App; ``` 6. 푸시 알림 전송 푸시 알림을 전송하기 위해 Firebase Cloud Messaging API를 사용할 수 있습니다. 서버에서 HTTP POST 요청을 통해 알림을 전송할 수 있습니다. 아래는 Node.js를 사용한 예시입니다. ```javascript const admin = require('<a href='https://sangseek.com/sangseeks/firebase-admin/ko'>firebase-admin</a>'); admin.initializeApp({ credential: admin.credential.applicationDefault(), }); const message = { notification: { title: 'Hello!', body: 'This is a test notification.', }, token: '<DEVICE_TOKEN>', }; admin.messaging().send(message) .then(response => { console.log('Successfully sent message:', response); }) .catch(error => { console.log('Error sending message:', error); }); ``` 7. 테스트 앱을 실행하고, Firebase Console에서 테스트 알림을 전송하여 푸시 알림이 제대로 수신되는지 확인합니다. 결론 리액트 네이티브에서 푸시 알림을 구현하는 과정은 여러 단계로 나뉘며, Firebase Cloud Messaging을 통해 쉽게 설정할 수 있습니다. 위의 단계를 따라가면 기본적인 푸시 알림 기능을 구현할 수 있으며, 필요에 따라 추가적인 기능을 확장할 수 있습니다.
이용안내
커뮤니티 이용안내
×
- 게시한 게시글로 발생하는 문제는 게시자에게 책임이 있습니다.
- 게시글이 타인/타업체의 저작권을 침해할 경우 모든 책임은 게시자에게 있습니다. 게시자가 모든 손해를 부담해야 합니다.
- 상식닷컴 운영자는 게시자와 상의하지 않고 게시글을 수정 또는 삭제할 수 있습니다.
- 상식닷컴 운영자는 깨끗한 커뮤니티 공간을 만드는 것이 1순위입니다.
수정하기
취소하기