0
0
Firebasecloud~10 mins

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

Choose your learning style9 modes available
Process Flow - Notification handling in foreground
App receives notification
Is app in foreground?
NoShow system notification
Yes
Handle notification in app
Update UI or alert user
End
When a notification arrives, the app checks if it is active (foreground). If yes, it handles the notification internally without showing a system alert.
Execution Sample
Firebase
firebase.messaging().onMessage((payload) => {
  console.log('Message received:', payload);
  alert('New message: ' + payload.notification.title);
});
This code listens for messages when the app is open and shows an alert with the notification title.
Process Table
StepEventApp StateAction TakenOutput
1Notification receivedForegroundCall onMessage handlerConsole logs message, alert shown
2User sees alertForegroundUser reads alertUI updated with message info
3Notification receivedBackgroundSystem shows notificationNotification appears in system tray
4User taps notificationBackgroundApp opens to relevant screenApp UI updated
5No more notifications--Waiting for next notification
💡 Execution stops waiting for new notifications or user actions.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
payloadnull{notification: {title: 'Hello'}}{notification: {title: 'Hello'}}{notification: {title: 'Hello'}}null
appStateForegroundForegroundForegroundBackgroundBackground
alertShownfalsetruetruefalsefalse
Key Moments - 2 Insights
Why does the app show an alert only when in foreground?
Because onMessage handler triggers only when app is active, as shown in execution_table step 1 and 2.
What happens if the app is in background when notification arrives?
The system shows the notification automatically, no alert from app code, as in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what action happens at step 3?
AApp shows alert with message
BApp logs message to console
CSystem shows notification in tray
DUser taps notification
💡 Hint
Check the 'Action Taken' column in step 3 of execution_table.
At which step does the app update the UI after user interaction?
AStep 2
BStep 4
CStep 1
DStep 5
💡 Hint
Look for 'App UI updated' in the 'Output' column.
If the app never goes to background, which steps will NOT occur?
ASteps 3 and 4
BStep 5 only
CSteps 1 and 2
DAll steps occur
💡 Hint
Steps 3 and 4 involve background state as per 'App State' column.
Concept Snapshot
Notification handling in foreground:
- Use onMessage listener to catch notifications when app is active.
- Show custom UI or alerts inside app.
- System notifications appear only if app is backgrounded.
- Helps avoid duplicate alerts and improves user experience.
- Always check app state before deciding notification display.
Full Transcript
When a notification arrives, the app first checks if it is in the foreground. If yes, it uses the onMessage handler to process the notification internally, such as showing an alert or updating the UI. If the app is in the background, the system automatically displays the notification in the system tray. The user can tap the notification to open the app and see relevant content. This approach prevents duplicate notifications and ensures a smooth user experience.