이번에 만드는 작은 사이드 프로젝트에서 Firebase를 이용한 Notification을 줄 일이 생겼다.
사용 과정과 오류 해결에 대해 간단히 적어보려고 한다.
@react-native-firebase/app
@react-native-firebase/messaging
useEffect(() => {
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
console.log(remoteMessage);
});
return unsubscribe;
}, []);
//filePath ./index.js
import 'expo-dev-client';
import { Alert, AppRegistry } from 'react-native';
import messaging from '@react-native-firebase/messaging';
import 'react-native-gesture-handler';
import { registerRootComponent } from 'expo';
import App from './App';
// Register background handler
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log('Message handled in the background!', remoteMessage);
Alert.alert('good');
});
// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
// It also ensures that whether you load the app in Expo Go or in a native build,
// the environment is set up appropriately
AppRegistry.registerComponent('app', () => App);
registerRootComponent(App);
android_channel_id
를 default로 설정하면 기본으로 보내진다 await fetch('https://fcm.googleapis.com/fcm/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `FCM 서버 키`,
},
body: JSON.stringify({
to: '디바이스 토큰',
data: {
title: 'test',
message: 'hihi',
android_channel_id: 'high-priority',
},
notification: {
title: '푸시테스트_notification',
body: 'good work',
sound: 'default',
android_channel_id: 'high-priority',
},
android: {
channel_id: 'high-priority',
},
priority: 'high',
}),
}).then((data) => console.log(data));