0
0
Firebasecloud~10 mins

Notification handling in background in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Notification handling in background
App receives notification
Is app in background?
NoShow notification in foreground
Yes
Background handler triggered
Process notification data
Show notification or update data silently
User taps notification?
NoWait
Yes
Open app or specific screen
When a notification arrives, the app checks if it is in background. If yes, a background handler processes it, showing notification or updating data silently. User interaction opens the app.
Execution Sample
Firebase
firebase.messaging().setBackgroundMessageHandler(async payload => {
  console.log('Message handled in the background!', payload);
  // Show notification or update data
});
This code sets a handler to process incoming notifications when the app is in the background.
Process Table
StepApp StateNotification ReceivedAction TakenResult
1ForegroundYesShow notification directlyUser sees notification immediately
2BackgroundYesTrigger background handlerHandler runs to process message
3BackgroundYesHandler processes dataNotification shown or data updated
4BackgroundUser taps notificationOpen app or specific screenApp opens to relevant content
5BackgroundUser does not tapWaitNo immediate app action
💡 Notification handled based on app state; background handler stops after processing.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
App StateForegroundBackgroundBackgroundBackgroundForeground (after open)
Notification ReceivedNoYesYesYesYes
Handler TriggeredNoYesYesYesNo (after open)
Notification ShownNoNoYesYesYes
App OpenedNoNoNoYesYes
Key Moments - 3 Insights
Why doesn't the notification show immediately when the app is in the background?
Because the background handler must first process the notification (see execution_table step 2 and 3), then it decides to show or update silently.
What happens if the user does not tap the notification?
The app stays in background and waits (execution_table step 5), no immediate action occurs until user interaction.
How does the app know to open a specific screen when the notification is tapped?
The background handler or notification payload includes data to navigate; tapping triggers app open with that data (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what action is taken when the app is in the foreground and a notification arrives?
ATrigger background handler
BShow notification directly
CWait silently
DOpen app screen
💡 Hint
Check step 1 in the execution_table where App State is Foreground.
At which step does the background handler process the notification data?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the Action Taken column in execution_table for processing data.
If the user never taps the notification, what is the app's state after step 5?
AForeground
BClosed
CBackground
DUnknown
💡 Hint
See execution_table step 5: app remains in Background.
Concept Snapshot
Notification handling in background:
- App checks if it is in foreground or background on notification arrival.
- Foreground: show notification immediately.
- Background: trigger background handler to process.
- Handler can show notification or update data silently.
- User tap opens app or specific screen.
Full Transcript
When a notification arrives, the app first checks if it is in the foreground or background. If the app is in the foreground, it shows the notification immediately. If the app is in the background, it triggers a background handler that processes the notification data. This handler can show a notification or update app data silently without user interaction. If the user taps the notification, the app opens to the relevant screen. If the user does not tap, the app remains in the background waiting for further action.