Handling push notifications across app states
State-aware notification handling.
foreground onMessage, background and quit setBackgroundMessageHandler and getInitialNotification, plus data-only messages and native background tasks like Background Fetch or Notification Service…
WHAT THIS TESTS Whether you know that notification handling branches by app state and that terminated-state and heavy processing require native mechanisms, not just JS handlers.
A GOOD ANSWER COVERS Foreground: the OS does not show a banner automatically, so you subscribe with onMessage (in Firebase Messaging) and decide whether to display an in-app banner or local notification and update state. Background and quit: a tap that opens the app is handled by onNotificationOpenedApp when backgrounded and by getInitialNotification when the app was terminated and launched by the tap, letting you deep-link. For messages that arrive while backgrounded or terminated and need processing without user interaction, you register setBackgroundMessageHandler, which runs a limited JS task triggered by a data message. Because the JS runtime is not freely running when terminated, complex background work leans on native capabilities: on iOS a Notification Service Extension can mutate or enrich a notification before display and a background mode or BGTask can do limited work; on Android a headless JS task or a foreground service handles longer processing. You distinguish notification messages, which the OS may display, from data-only messages, which wake your handler.
COMMON WRONG ANSWERS Assuming onMessage fires in all states; it only fires in the foreground. Expecting arbitrary JS to run when the app is terminated. Confusing onNotificationOpenedApp (backgrounded) with getInitialNotification (terminated launch). Forgetting that foreground notifications need manual display. Trying to do heavy work in setBackgroundMessageHandler beyond its short time budget.
LIKELY FOLLOW-UPS How do data-only versus notification messages differ, what are the time limits on background handlers, how does an iOS Notification Service Extension work, and how do you deep-link from a cold start.
ONE CONCRETE EXAMPLE A messaging app: in the foreground onMessage shows a custom in-app banner. When backgrounded, a data message triggers setBackgroundMessageHandler to update a local unread count. When the user taps a notification that launched a terminated app, getInitialNotification returns the payload at startup so you navigate straight to the relevant chat. For decrypting or downloading an attachment before display on iOS, a Notification Service Extension processes the payload natively before the banner appears.
Read the original → rnfirebase.io
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.