Complete the code to register the background message handler.
import { getMessaging, onBackgroundMessage } from 'firebase/messaging/sw'; const messaging = getMessaging(); onBackgroundMessage(messaging, (payload) => { console.log('Received background message ', [1]); });
The payload parameter contains the message data received in the background.
Complete the code to show a notification with title and body from the payload.
self.registration.showNotification(payload.notification.[1], {
body: payload.notification.body
});The notification title is accessed via payload.notification.title.
Fix the error in the background message handler to correctly access notification data.
onBackgroundMessage(messaging, (payload) => {
const notificationTitle = payload.[1].title;
const notificationOptions = {
body: payload.notification.body
};
self.registration.showNotification(notificationTitle, notificationOptions);
});payload.data.title instead of payload.notification.title.The notification data is inside the notification property of the payload.
Fill both blanks to correctly extract title and body from the payload and show notification.
const title = payload.[1].title; const options = { body: payload.[2].body }; self.registration.showNotification(title, options);
data and notification properties.Both title and body come from the notification property of the payload.
Fill all three blanks to handle background message, extract title and body, and show notification.
onBackgroundMessage(messaging, ([1]) => { const title = [1].[2].title; const options = { body: [1].[2].body }; self.registration.showNotification(title, options); });
data with notification.The callback parameter is payload. The notification details are inside payload.notification.