native firebase message / expo-notification

코듀잇·2025년 5월 30일

ReactNative

목록 보기
2/3

알림 이벤트 리스너

useEffect(() => {
  registerForPushNotificationsAsync().then(token => setExpoPushToken(token));

  notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
    setNotification(notification);
  });

  responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
    console.log(response);
  });

  return () => {
    Notifications.removeNotificationSubscription(notificationListener.current);
    Notifications.removeNotificationSubscription(responseListener.current);
  };
}, []);

포그라운드 알림 동작

Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: false,
    shouldSetBadge: false,
  }),
  1. 메시지 구독(Message Subscribe)
    💡 메시지 구독(Message Subscribe)
  • FCM으로부터 메시지를 전달받을 수 있도록 메시지 구독이 필요합니다. 이 메시지 구독은 두 가지 메서드로 제공을 합니다.
    메서드 설명
    onMessage() 애플리케이션이 활성 상태이거나 포그라운드에 있을 때 FCM 메시지를 처리 함수
    setBackgroundMessageHandler() 앱이 종료된 상태일 때 FCM 메시지를 처리 함수
import messaging from '@react-native-firebase/messaging';

// Note that an async function or a function that returns a Promise 
// is required for both subscribers.
const onMessageReceived = async (message: FirebaseMessagingTypes.RemoteMessage) => {
	// Do something
}

const unsubscribe = messaging().onMessage(async remoteMessage => onMessageReceived(remoteMessage));  	// 활성 상태 및 포그라운드 상태일때 FCM 메시지 수신
messaging().setBackgroundMessageHandler(async remoteMessage => onMessageReceived(remoteMessage));       // 앱이 종료된 상태일 때 FCM 메시지 수신
profile
네이버 블로그: https://blog.naver.com/coduit

0개의 댓글