firebase-messaging-sw.js
// firebase-messaging-sw.js
importScripts('https://www.gstatic.com/firebasejs/10.13.0/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/10.13.0/firebase-messaging-compat.js');
// Your web app's Firebase configuration (same as in your firebase.js)
const firebaseConfig = {
apiKey: "your-api-key",
authDomain: "your-auth-domain",
projectId: "your-project-id",
storageBucket: "your-storage-bucket",
messagingSenderId: "your-messaging-sender-id",
appId: "your-app-id",
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Retrieve Firebase Messaging object.
const messaging = firebase.messaging();
messaging.onBackgroundMessage(function (payload) {
console.log('Received background message ', payload);
// Extract notification data
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
icon: payload.notification.icon || "/img/push-noti-icon.png",
badge: "/img/push-badge-icon.png",
image: "/img/push-image.png",
data: { url: payload.notification.click_action || 'https://velog.io/@inetsos/posts' } // Custom data for the click event
};
self.registration.showNotification(notificationTitle, notificationOptions);
});