|
| 1 | +/* global firebase */ |
| 2 | +import { FIREBASE_CONFIG, VAPID_KEY } from './modules/constants'; |
| 3 | + |
| 4 | +const toggler = document.querySelector('.navbar-toggle'); |
| 5 | + |
| 6 | +if (toggler) { |
| 7 | + const target = document.querySelector(toggler.dataset.target); |
| 8 | + toggler.addEventListener('click', (e) => { |
| 9 | + e.preventDefault(); |
| 10 | + const opened = !toggler.classList.contains('collapsed'); |
| 11 | + toggler.classList[opened ? 'add' : 'remove']('collapsed'); |
| 12 | + toggler.setAttribute('aria-expanded', String(!opened)); |
| 13 | + target.classList[opened ? 'remove' : 'add']('in'); |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +async function subscribe(sw) { |
| 18 | + const result = await Notification.requestPermission(); |
| 19 | + |
| 20 | + if (result !== 'granted' || !window.firebase) { |
| 21 | + return; |
| 22 | + } |
| 23 | + // Initialize Firebase |
| 24 | + |
| 25 | + firebase.initializeApp(FIREBASE_CONFIG); |
| 26 | + |
| 27 | + const messaging = firebase.messaging(); |
| 28 | + |
| 29 | + try { |
| 30 | + const token = await messaging.getToken({ |
| 31 | + serviceWorkerRegistration: sw, |
| 32 | + vapidKey: VAPID_KEY, |
| 33 | + }); |
| 34 | + if (!token) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + console.log(token); |
| 39 | + document.getElementById('token').textContent = token; |
| 40 | + } catch (err) { |
| 41 | + console.error('An error occurred while retrieving token. ', err); |
| 42 | + } |
| 43 | + |
| 44 | + messaging.onMessage((payload) => { |
| 45 | + console.log('Message received. ', payload); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +// async function notify() { |
| 50 | +// const result = await Notification.requestPermission(); |
| 51 | +// if (result !== 'granted') { |
| 52 | +// return; |
| 53 | +// } |
| 54 | +// const body = 'hello'; |
| 55 | +// const icon = '/apple-touch-icon.png'; |
| 56 | +// new Notification('Hello!', { |
| 57 | +// body, |
| 58 | +// icon, |
| 59 | +// }); |
| 60 | +// } |
| 61 | + |
| 62 | +function registerNotifier(sw) { |
| 63 | + const hideNotificationBar = |
| 64 | + localStorage.getItem('notificationBar') === 'hide'; |
| 65 | + if (!hideNotificationBar) { |
| 66 | + const bar = document.getElementById('notification-bar'); |
| 67 | + |
| 68 | + const closeBar = () => { |
| 69 | + bar.hidden = true; |
| 70 | + localStorage.setItem('notificationBar', 'hide'); |
| 71 | + }; |
| 72 | + |
| 73 | + bar.hidden = false; |
| 74 | + bar.querySelector('button.close').addEventListener('click', closeBar); |
| 75 | + |
| 76 | + bar.querySelector('.btn.btn-link').addEventListener('click', async () => { |
| 77 | + const result = await Notification.requestPermission(); |
| 78 | + closeBar(); |
| 79 | + if (result === 'granted') { |
| 80 | + subscribe(sw); |
| 81 | + } |
| 82 | + }); |
| 83 | + } else { |
| 84 | + subscribe(sw); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +if (process.env.NODE_ENV === 'production') { |
| 89 | + if ('serviceWorker' in navigator) { |
| 90 | + navigator.serviceWorker |
| 91 | + .register('/service-worker.js') |
| 92 | + .then(registerNotifier); |
| 93 | + } |
| 94 | +} else { |
| 95 | + registerNotifier(); |
| 96 | +} |
0 commit comments