From 1b1888d4fd7778847e0f38f250ea3a4ea6a97d6a Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 31 Mar 2021 14:25:58 +0200 Subject: [PATCH] [notification] Disable native is not available --- src/layouts/MainLayout.vue | 18 +++++++++++++++--- src/stores/index.ts | 19 +++++++++---------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/layouts/MainLayout.vue b/src/layouts/MainLayout.vue index a3857f6..d32876e 100644 --- a/src/layouts/MainLayout.vue +++ b/src/layouts/MainLayout.vue @@ -25,7 +25,7 @@ >{{ notifications.length }} @@ -131,8 +131,9 @@ export default defineComponent({ const shortcuts = flaschengeist?.shortcuts || []; const mainLinks = flaschengeist?.menuLinks || []; const notifications = computed(() => mainStore.notifications.slice().reverse()); - const noPermission = ref(window.Notification.permission !== 'granted'); const polling = ref(NaN); + const useNative = 'Notification' in window && window.Notification !== undefined; + const noPermission = ref(!useNative || window.Notification.permission !== 'granted'); onBeforeMount(() => pollNotification()); onBeforeUnmount(() => window.clearInterval(polling.value)); @@ -170,7 +171,17 @@ export default defineComponent({ function pollNotification() { polling.value = window.setInterval(() => { - void mainStore.loadNotifications(flaschengeist); + void mainStore + .loadNotifications(flaschengeist) + .then((notifications) => { + if (useNative && !noPermission.value) + notifications.forEach( + (notif) => + new window.Notification(notif.text, { + timestamp: notif.time.getTime(), + }) + ); + }); }, config.pollingInterval); } @@ -187,6 +198,7 @@ export default defineComponent({ requestPermission, shortcuts, subLinks, + useNative, }; }, }); diff --git a/src/stores/index.ts b/src/stores/index.ts index 361dfc5..089fc3c 100644 --- a/src/stores/index.ts +++ b/src/stores/index.ts @@ -114,19 +114,18 @@ export const useMainStore = defineStore({ ? { from: this.notifications[this.notifications.length - 1].time } : {}; const { data } = await api.get('/notifications', { params: params }); + const notifications: FG_Plugin.Notification[] = []; data.forEach((n) => { n.time = new Date(n.time); - const notif = ( - flaschengeist?.plugins.filter((p) => p.name === n.plugin)[0]?.notification || - translateNotification - )(n); - this.notifications.push(notif); - - if (window.Notification.permission === 'granted') - new window.Notification(notif.text, { - timestamp: notif.time.getTime(), - }); + notifications.push( + ( + flaschengeist?.plugins.filter((p) => p.name === n.plugin)[0]?.notification || + translateNotification + )(n) + ); }); + this.notifications.push(...notifications); + return notifications; }, async removeNotification(id: number) {