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,29 @@ 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+ whenever (() => authStore .currentUser , async (currentUser ) => {
36+ if (Notification .permission === ' granted' && currentUser ) {
37+ const token = await getToken (getMessaging (), { vapidKey: config .public .firebase .keyPair })
38+
39+ notificationApi .createTokenDevice (token )
40+ }
41+ })
42+ }
43+ catch {}
2944 }
3045})
3146 </script >
0 commit comments