From 48acf4211d4ea2ce7f2e5ba773744a1f3b0809e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sedat=20=C3=96ZT=C3=9CRK?= <76204082+iamsedatozturk@users.noreply.github.com> Date: Fri, 15 May 2026 15:04:47 +0300 Subject: [PATCH] =?UTF-8?q?Notification=20d=C3=BCzenlemesi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/.gitignore | 7 +- ui/src/components/template/Notification.tsx | 148 +++++++++----------- 2 files changed, 69 insertions(+), 86 deletions(-) diff --git a/api/.gitignore b/api/.gitignore index 17718b7..7e9978f 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -267,4 +267,9 @@ src/Sozsoft.Platform.Blazor.Server.Tiered/Logs/* # DevExpress License - DO NOT commit to repository **/DevExpress_License.txt -DevExpress_License.txt \ No newline at end of file +DevExpress_License.txt + +# Local cache files +*.lscache +lscache/ +**/lscache/ \ No newline at end of file diff --git a/ui/src/components/template/Notification.tsx b/ui/src/components/template/Notification.tsx index 6a223b8..49dc77b 100644 --- a/ui/src/components/template/Notification.tsx +++ b/ui/src/components/template/Notification.tsx @@ -76,9 +76,8 @@ const _Notification = ({ className }: { className?: string }) => { const [loading, setLoading] = useState(false) const toastNotificationList = useRef([]) - const toastNotificationInterval = useRef>() const desktopNotificationList = useRef([]) - const desktopNotificationInterval = useRef>() + const pushNotificationInterval = useRef>() const { bgTheme } = useThemeClass() const { larger } = useResponsive() @@ -103,7 +102,9 @@ const _Notification = ({ className }: { className?: string }) => { useEffect(() => { getReactNotificationCount() var intervalId = setInterval(() => { - getReactNotificationCount() + if (tabHasFocusRef.current) { + getReactNotificationCount() + } }, notificationInterval) return () => { @@ -174,114 +175,91 @@ const _Notification = ({ className }: { className?: string }) => { [notificationList], ) - // Toast - const getToastNotifications = async () => { + const getPushNotifications = async () => { + const desktopGranted = + 'Notification' in window && window.Notification.permission === 'granted' + + const channels = [NotificationChannels.UiToast] + if (desktopGranted) channels.push(NotificationChannels.Desktop) + const resp = await getList({ - channels: [NotificationChannels.UiToast], + channels, isListRequest: false, isSent: false, maxResultCount: 1000, }) const items = resp.data.items ?? [] - const newNotificationList = items.filter( - (a) => !toastNotificationList.current.includes(a.id) && !a.isSent, + + // Toast + const newToastList = items.filter( + (a) => + a.notificationChannel === NotificationChannels.UiToast && + !toastNotificationList.current.includes(a.id) && + !a.isSent, ) toastNotificationList.current = [ ...toastNotificationList.current, - ...newNotificationList.map((a) => a.id), + ...newToastList.map((a) => a.id), ] - for (const notification of newNotificationList) { + for (const notification of newToastList) { toast.push( - + {notification.message} , - { - placement: 'bottom-end', - }, + { placement: 'bottom-end' }, ) - await updateSent(notification.id, true) await updateRead(notification.id, true) } + + // Desktop + if (desktopGranted) { + const newDesktopList = items.filter( + (a) => + a.notificationChannel === NotificationChannels.Desktop && + !desktopNotificationList.current.includes(a.id) && + !a.isSent, + ) + desktopNotificationList.current = [ + ...desktopNotificationList.current, + ...newDesktopList.map((a) => a.id), + ] + for (const notification of newDesktopList) { + const title = notification.notificationType || 'Bildirim' + const options = { + body: notification.message, + dir: 'ltr', + requireInteraction: true, + } as NotificationOptions + + if ('serviceWorker' in navigator && navigator.serviceWorker.controller) { + const reg = await navigator.serviceWorker.ready + await reg.showNotification(title, options) + } else { + new window.Notification(title, options) + } + + await updateSent(notification.id, true) + await updateRead(notification.id, true) + } + } } useEffect(() => { - if (tabHasFocusRef.current) { - getToastNotifications() + if ('Notification' in window && window.Notification.permission === 'default') { + window.Notification.requestPermission() } - toastNotificationInterval.current = setInterval(async () => { + + getPushNotifications() + + pushNotificationInterval.current = setInterval(() => { if (tabHasFocusRef.current) { - await getToastNotifications() + getPushNotifications() } }, notificationInterval) return () => { - clearInterval(toastNotificationInterval.current) - } - }, []) - - //Desktop - const getDesktopNotifications = async () => { - if (!('Notification' in window) || window.Notification.permission !== 'granted') return - - const resp = await getList({ - channels: [NotificationChannels.Desktop], - isListRequest: false, - isSent: false, - maxResultCount: 1000, - }) - const items = resp.data.items ?? [] - const newNotificationList = items.filter( - (a) => !desktopNotificationList.current.includes(a.id) && !a.isSent, - ) - desktopNotificationList.current = [ - ...desktopNotificationList.current, - ...newNotificationList.map((a) => a.id), - ] - for (const notification of newNotificationList) { - const title = notification.notificationType || 'Bildirim' - const options = { - body: notification.message, - dir: 'ltr', - requireInteraction: true, - } as NotificationOptions - - if ('serviceWorker' in navigator && navigator.serviceWorker.controller) { - const reg = await navigator.serviceWorker.ready - await reg.showNotification(title, options) - } else { - new window.Notification(title, options) - } - - await updateSent(notification.id, true) - await updateRead(notification.id, true) - } - } - - useEffect(() => { - const startDesktopNotifications = async () => { - if (!('Notification' in window)) return - - if (window.Notification.permission === 'default') { - await window.Notification.requestPermission() - } - - if (window.Notification.permission === 'granted') { - await getDesktopNotifications() - desktopNotificationInterval.current = setInterval(async () => { - await getDesktopNotifications() - }, notificationInterval) - } - } - - startDesktopNotifications() - - return () => { - clearInterval(desktopNotificationInterval.current) + clearInterval(pushNotificationInterval.current) } }, [])