|
| 1 | +/// <reference lib="webworker" /> |
| 2 | +/* eslint-env serviceworker */ |
| 3 | +/* global firebase importScripts clients */ |
| 4 | +importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-app-compat.js'); |
| 5 | +importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-messaging-compat.js'); |
| 6 | + |
| 7 | +firebase.initializeApp({ |
| 8 | + apiKey: 'AIzaSyAjZqK2lhCOeX_P2Sf-_2IGEFlORchcO5w', |
| 9 | + authDomain: 'withtime-ff471.firebaseapp.com', |
| 10 | + projectId: 'withtime-ff471', |
| 11 | + storageBucket: 'withtime-ff471.firebasestorage.app', |
| 12 | + messagingSenderId: '47995224236', |
| 13 | + appId: '1:47995224236:web:85371605ce4a6659529f09', |
| 14 | + measurementId: 'G-5E8Q23LL4H', |
| 15 | +}); |
| 16 | + |
| 17 | +const messaging = firebase.messaging(); |
| 18 | + |
| 19 | +self.addEventListener('push', function (event) { |
| 20 | + try { |
| 21 | + const payload = event.data.json(); |
| 22 | + const title = payload.notification.title; |
| 23 | + |
| 24 | + const options = { |
| 25 | + body: payload.notification.body, |
| 26 | + icon: payload.notification.icon, |
| 27 | + data: payload.notification.click_action, |
| 28 | + }; |
| 29 | + |
| 30 | + event.waitUntil(self.registration.showNotification(title, options)); |
| 31 | + } catch (error) { |
| 32 | + console.error('Push event error:', error); |
| 33 | + } |
| 34 | +}); |
| 35 | + |
| 36 | +self.addEventListener('notificationclick', function (event) { |
| 37 | + console.log(event.notification); |
| 38 | + |
| 39 | + event.notification.close(); |
| 40 | + |
| 41 | + event.waitUntil(clients.openWindow(event.notification.data).catch((error) => console.error('Failed to open window:', error))); |
| 42 | +}); |
0 commit comments