Bird
Raised Fist0
Firebasecloud~20 mins

Notification handling in background in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Notification Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
How does Firebase Cloud Messaging handle notifications when the app is in the background on Android?

When a Firebase Cloud Messaging (FCM) notification is sent to an Android device and the app is in the background, what happens to the notification?

AThe notification is automatically displayed in the system tray without invoking app code.
BThe app receives the notification data immediately and processes it in the background service.
CThe notification is discarded and not shown to the user.
DThe app wakes up and shows a custom notification UI immediately.
Attempts:
2 left
💡 Hint

Think about how Android handles notifications when the app is not active.

❓ Architecture
intermediate
2:00remaining
Which Firebase service is best suited to trigger background notification handling logic?

You want to run custom code in the background when a notification arrives, even if the app is closed. Which Firebase service should you use?

AFirebase Cloud Functions triggered by Firestore changes
BFirebase Cloud Messaging data messages handled by a background service
CFirebase Realtime Database listeners in the foreground app
DFirebase Authentication triggers
Attempts:
2 left
💡 Hint

Consider which message type allows background processing on the device.

❓ security
advanced
2:00remaining
What is a security risk when handling background notifications with data messages in Firebase?

When using Firebase Cloud Messaging data messages to handle notifications in the background, what security risk should you be aware of?

AData messages automatically expose user credentials.
BNotifications can only be sent to authenticated users.
CBackground services cannot access the internet.
DData messages can be intercepted and modified if not encrypted.
Attempts:
2 left
💡 Hint

Think about data transmission security over networks.

❓ Configuration
advanced
2:00remaining
Which manifest permission is required for background notification handling on Android with Firebase?

To receive Firebase Cloud Messaging data messages in the background on Android, which permission must be declared in the AndroidManifest.xml?

A<uses-permission android:name="android.permission.WAKE_LOCK" />
B<uses-permission android:name="android.permission.INTERNET" />
C<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
D<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Attempts:
2 left
💡 Hint

Consider what permission allows the app to keep the device awake to process messages.

✅ Best Practice
expert
2:00remaining
What is the best practice to ensure reliable background notification handling on iOS with Firebase?

On iOS, background notification handling is limited. Which approach best ensures your app processes notifications reliably in the background?

ADisable background modes to save battery and rely on user opening the app.
BUse local notifications scheduled by the app instead of remote notifications.
CUse silent push notifications with content-available flag and enable background fetch capability.
DSend only notification messages without data payload to wake the app.
Attempts:
2 left
💡 Hint

Think about how iOS allows apps to run code in the background.

Practice

(1/5)
1. What is the main purpose of using onBackgroundMessage in Firebase Cloud Messaging?
easy
A. To authenticate users before sending notifications
B. To send messages from the client to the server
C. To handle notifications when the app is not open or in the background
D. To delete old notifications from the device

Solution

  1. Step 1: Understand the role of onBackgroundMessage

    This function is designed to catch messages when the app is not active or open.
  2. Step 2: Compare with other options

    Sending messages, authentication, or deleting notifications are unrelated to background message handling.
  3. Final Answer:

    To handle notifications when the app is not open or in the background -> Option C
  4. Quick Check:

    Background message handling = To handle notifications when the app is not open or in the background [OK]
Hint: Remember: background means app closed or hidden [OK]
Common Mistakes:
  • Confusing sending messages with receiving them
  • Thinking it manages user authentication
  • Assuming it deletes notifications automatically
2. Which of the following is the correct way to import and use onBackgroundMessage in a Firebase service worker?
easy
A. import { onBackgroundMessage } from 'firebase/messaging/sw'; onBackgroundMessage(handler);
B. import { onBackgroundMessage } from 'firebase/messaging/sw'; onBackgroundMessage(messaging, handler);
C. import { onBackgroundMessage } from 'firebase/messaging'; messaging.onBackgroundMessage(handler);
D. import { onBackgroundMessage } from 'firebase/messaging'; onBackgroundMessage(messaging, handler);

Solution

  1. Step 1: Identify correct import path

    The correct import for service worker usage is from 'firebase/messaging/sw'.
  2. Step 2: Use correct function signature

    onBackgroundMessage requires only the handler function as argument (no messaging instance needed).
  3. Final Answer:

    import { onBackgroundMessage } from 'firebase/messaging/sw'; onBackgroundMessage(handler); -> Option A
  4. Quick Check:

    Correct import and call signature = import { onBackgroundMessage } from 'firebase/messaging/sw'; onBackgroundMessage(handler); [OK]
Hint: Remember: import from 'firebase/messaging/sw' and pass handler only [OK]
Common Mistakes:
  • Importing from wrong path like 'firebase/messaging'
  • Passing messaging instance to onBackgroundMessage
  • Using messaging.onBackgroundMessage(handler)
3. Given this service worker code snippet, what will happen when a background message arrives?
import { onBackgroundMessage } from 'firebase/messaging/sw';
import { messaging } from './firebase-config';

onBackgroundMessage((payload) => {
  console.log('Received background message ', payload);
  self.registration.showNotification(payload.notification.title, {
    body: payload.notification.body
  });
});
medium
A. Only the message payload is logged, no notification is shown
B. The notification shows but the payload is not logged
C. An error occurs because showNotification is not a valid method
D. The message payload is logged and a notification with title and body is shown

Solution

  1. Step 1: Analyze the callback function

    The callback logs the payload and calls showNotification with title and body from the payload.
  2. Step 2: Confirm method validity

    self.registration.showNotification is the correct way to show notifications in service workers.
  3. Final Answer:

    The message payload is logged and a notification with title and body is shown -> Option D
  4. Quick Check:

    Payload logged + notification shown = The message payload is logged and a notification with title and body is shown [OK]
Hint: Check if both console.log and showNotification are called [OK]
Common Mistakes:
  • Thinking showNotification is invalid in service workers
  • Assuming logging disables notification
  • Ignoring the payload content usage
4. You wrote this service worker code but notifications do not appear when the app is in background:
import { onBackgroundMessage } from 'firebase/messaging';
import { messaging } from './firebase-config';

onBackgroundMessage((payload) => {
  self.registration.showNotification(payload.notification.title, {
    body: payload.notification.body
  });
});
What is the main issue?
medium
A. Using self.registration.showNotification instead of Notification.show
B. Wrong import path for onBackgroundMessage ('firebase/messaging' instead of 'firebase/messaging/sw')
C. Not importing messaging from Firebase
D. Payload does not contain notification object

Solution

  1. Step 1: Check onBackgroundMessage usage

    The import path is wrong for service worker; must be from 'firebase/messaging/sw'.
  2. Step 2: Verify notification method and imports

    self.registration.showNotification is correct and messaging is imported but not needed.
  3. Final Answer:

    Wrong import path for onBackgroundMessage ('firebase/messaging' instead of 'firebase/messaging/sw') -> Option B
  4. Quick Check:

    Service worker import path = Wrong import path for onBackgroundMessage ('firebase/messaging' instead of 'firebase/messaging/sw') [OK]
Hint: Remember: import from 'firebase/messaging/sw' [OK]
Common Mistakes:
  • Using wrong import path for onBackgroundMessage
  • Confusing notification API names
  • Assuming payload structure is always wrong
5. You want to ensure your Firebase app shows a notification with a custom icon and vibrates when a background message arrives. Which code snippet correctly adds these features inside onBackgroundMessage?
hard
A. onBackgroundMessage((payload) => { self.registration.showNotification(payload.notification.title, { body: payload.notification.body, icon: '/icon.png', vibrate: [100, 50, 100] }); });
B. onBackgroundMessage((payload) => { self.showNotification(payload.notification.title, { body: payload.notification.body, icon: '/icon.png', vibrate: [100, 50, 100] }); });
C. onBackgroundMessage((payload) => { Notification.show(payload.notification.title, { body: payload.notification.body, icon: '/icon.png', vibrate: [100, 50, 100] }); });
D. onBackgroundMessage((payload) => { self.registration.showNotification(payload.title, { body: payload.body, icon: '/icon.png', vibrate: [100, 50, 100] }); });

Solution

  1. Step 1: Use correct notification method and parameters

    self.registration.showNotification is correct. The notification title and body come from payload.notification.
  2. Step 2: Add icon and vibrate options properly

    Options icon and vibrate are correctly included in the options object.
  3. Final Answer:

    onBackgroundMessage((payload) => { self.registration.showNotification(payload.notification.title, { body: payload.notification.body, icon: '/icon.png', vibrate: [100, 50, 100] }); }); -> Option A
  4. Quick Check:

    Correct method, payload fields, and options = onBackgroundMessage((payload) => { self.registration.showNotification(payload.notification.title, { body: payload.notification.body, icon: '/icon.png', vibrate: [100, 50, 100] }); }); [OK]
Hint: Use self.registration.showNotification with full payload.notification fields [OK]
Common Mistakes:
  • Using wrong notification method like self.showNotification
  • Accessing payload.title instead of payload.notification.title
  • Omitting icon or vibrate options
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard