45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
/**
|
|
* Firebase Cloud Messaging Service Worker
|
|
* VITE_FIREBASE_* 는 빌드 시 index.html 주입 또는 수동 설정
|
|
*/
|
|
importScripts('https://www.gstatic.com/firebasejs/10.7.1/firebase-app-compat.js');
|
|
importScripts('https://www.gstatic.com/firebasejs/10.7.1/firebase-messaging-compat.js');
|
|
|
|
const firebaseConfig = {
|
|
apiKey: 'YOUR_API_KEY',
|
|
authDomain: 'YOUR_PROJECT.firebaseapp.com',
|
|
projectId: 'YOUR_PROJECT_ID',
|
|
storageBucket: 'YOUR_PROJECT.appspot.com',
|
|
messagingSenderId: 'YOUR_SENDER_ID',
|
|
appId: 'YOUR_APP_ID',
|
|
};
|
|
|
|
if (firebaseConfig.apiKey && firebaseConfig.apiKey !== 'YOUR_API_KEY') {
|
|
firebase.initializeApp(firebaseConfig);
|
|
const messaging = firebase.messaging();
|
|
|
|
messaging.onBackgroundMessage((payload) => {
|
|
const title = payload.notification?.title || 'GoldenChart';
|
|
const body = payload.notification?.body || payload.data?.message || '새 알림';
|
|
self.registration.showNotification(title, {
|
|
body,
|
|
icon: '/favicon.ico',
|
|
tag: 'goldenchart-fcm',
|
|
data: payload.data || {},
|
|
});
|
|
});
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close();
|
|
const url = (event.notification.data && event.notification.data.page) || '/';
|
|
event.waitUntil(
|
|
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(list => {
|
|
for (const c of list) {
|
|
if (c.url.includes(self.location.origin) && 'focus' in c) return c.focus();
|
|
}
|
|
if (clients.openWindow) return clients.openWindow(self.location.origin + url);
|
|
}),
|
|
);
|
|
});
|
|
}
|