diff --git a/frontend/app.vue b/frontend/app.vue index adf2312..fbb6710 100644 --- a/frontend/app.vue +++ b/frontend/app.vue @@ -36,14 +36,31 @@ const userAllPersons = ref([]) const calEvents = ref([]) const news = ref(null) -const messaging = getMessaging() +const messaging = ref(null) onMounted(() => { auth.value = getAuth() if (process.client) { if ('serviceWorker' in navigator && window.isSecureContext) { - navigator.serviceWorker.register('/sw.js'); + messaging.value = getMessaging() + navigator.serviceWorker.register('/sw.js').catch(e => alert(e)); + + onMessage(messaging.value, (payload) => { + console.log('Message received. ', payload); + + const { show } = useWebNotification({ + title: payload.notification.title, + body: payload.notification.body, + icon: '/ios/256.png', + dir: 'auto', + lang: 'en', + renotify: true, + tag: 'wrbapp', + }) + + show() + }); } } @@ -78,22 +95,6 @@ onMounted(() => { }) }) -onMessage(messaging, (payload) => { - console.log('Message received. ', payload); - - const { show } = useWebNotification({ - title: payload.notification.title, - body: payload.notification.body, - icon: '/ios/256.png', - dir: 'auto', - lang: 'en', - renotify: true, - tag: 'wrbapp', - }) - - show() -}); - const getPersons = async (persons) => { userPersons.value = []; @@ -152,9 +153,7 @@ const setupNotifications = () => { } const registerFCM = () => { - const messaging = getMessaging() - - getToken(messaging, { vapidKey: 'BI7l3nyGV6wJcFh7wrwmQ42W7RSXl46bmhXZJmDd4P-0K_JFP0ClTqjO-rr5H5DXBbmVR4kXwxFpUlo_d6cUy4Q' }).then((currentToken) => { + getToken(messaging.value, { vapidKey: 'BI7l3nyGV6wJcFh7wrwmQ42W7RSXl46bmhXZJmDd4P-0K_JFP0ClTqjO-rr5H5DXBbmVR4kXwxFpUlo_d6cUy4Q' }).then((currentToken) => { if (currentToken) { console.log(currentToken) } else { diff --git a/frontend/plugins/firebase.js b/frontend/plugins/firebase.js index 093813d..a05342d 100644 --- a/frontend/plugins/firebase.js +++ b/frontend/plugins/firebase.js @@ -15,6 +15,9 @@ export default defineNuxtPlugin((nuxtApp) => { const app = initializeApp(firebaseConfig); - const messaging = getMessaging(app); + try { + const messaging = getMessaging(app); + } catch {} + const analytics = getAnalytics(app); }) \ No newline at end of file