11<script setup lang="ts">
22import { useTheme } from ' vuetify'
3- import { getMessaging , onMessage } from ' firebase/messaging'
3+ import { getMessaging , getToken } from ' firebase/messaging'
44import ScrollToTop from ' @base/@core/components/ScrollToTop.vue'
55import initCore from ' @base/@core/initCore'
66import { initConfigStore , useConfigStore } from ' @base/@core/stores/config'
@@ -18,14 +18,27 @@ const { global } = useTheme()
1818if (isMobile )
1919 configStore .appContentLayoutNav = ' vertical'
2020
21- onBeforeMount (async () => {
22- if (! isInAppBrowser ()) {
23- onMessage (getMessaging (), () => {
24- // TODO: Handle incoming messages
25- // console.log('Client message:', payload)
26- // const linkSplits = payload.fcmOptions?.link?.split('/projects/')
27- // notify(payload.notification?.body as string, { type: 'primary', link: `/projects/${linkSplits![1]}` })
28- })
21+ const config = useRuntimeConfig ()
22+
23+ const tokenDevice = useLocalStorage <string | null >(' tokenDevice' , null )
24+
25+ const notificationApi = useApiNotification ()
26+
27+ onMounted (async () => {
28+ if (! isInAppBrowser () && ! tokenDevice .value ) {
29+ try {
30+ if (Notification .permission !== ' granted' )
31+ await Notification .requestPermission ()
32+
33+ const authStore = useAuthStore ()
34+
35+ if (Notification .permission === ' granted' && authStore .currentUser ) {
36+ const token = await getToken (getMessaging (), { vapidKey: config .public .firebase .keyPair })
37+
38+ notificationApi .createTokenDevice (token )
39+ }
40+ }
41+ catch {}
2942 }
3043})
3144 </script >
0 commit comments