[Fix] FCM iOS onNotificationOpenedApp, getInitialNotification 핸들러가 작동하지 않을 때

장은봉·2023년 2월 17일
1

React Native

목록 보기
2/5

❌ 증상

  • 공통
    • react-native@0.70.6
    • firebase/app, firebase/messaging ^16.7.0
    • notifee ^7.4.0
  • iOS
    • messaging().onNotificationOpenedApp, messaging().getInitialNotification 핸들러가 작동하지 않음
    • messaging().onMessage, root에 위치한 messaging().setBackgroundMessageHandler는 정상 작동함

🕵️‍♂️ 원인

  • 모종의 이유로 FCM과 notifee를 둘 다 설치했을 때
    FCM의 onNotificationOpenedApp, getInitialNotification은 Notifee의 onForegroudMessage로 통합된다.
    (FCM의 핸들러는 작동하지 않는다)
  • Notifee 삭제 후 빌드 시, 다시 FCM의 핸들러가 작동하게된다.
  • Foreground라는 명칭 때문에 혼란이 있었다. (onForegroundMessage === onMessage라고 생각했기 때문)
  • Notifee의 firebase/messaging intergration 문서가 있지만 위 내용은 없었다.

🩹 해결

  • FCM의 각 핸들러는 다음과 같은 일을 한다
    • onNotificationOpenedApp : AppState가 background일 때, 알림을 엶
    • getInitialOpenedApp : AppState가 terminated(exit)일 때, 알림을 엶
  • Notifee가 설치되었다면 위 두 핸들러가 Notifee.onForegroundMessage로 통합되어,
    분리해서 생각할 필요없이 '알림을 엶으로써 앱이 Focus되었을 때'라고만 생각하고 할당하면 된다.

✔️ 염두해둘 것

  • 작업을 하다보니 알게된 것이지만 Android는 반대로 FCM의 핸들러가 작동되고 Notifee의 핸들러가 작동하지 않는다.
    고로 플랫폼 별 핸들러 할당을 분기해야했다.
  • FCM 문서에서 Notifee 사용을 권장하기도 하던데 두 라이브러리가 의도한 작업 방향인지는 잘 모르겠다.

2개의 댓글

comment-user-thumbnail
2023년 8월 17일

감사합니다! 안드로이드에서 quit 상태에서 푸시 알림 PRESS 처리할 수가 없었는데, FCM 핸들러를 썼어야 했군요,,, 많이 배워 갑니다!

답글 달기
comment-user-thumbnail
2023년 10월 19일

덕분에 시간 절약했습니다. 감사합니다!

답글 달기