Notification düzenlemesi
This commit is contained in:
parent
f62d4b1a74
commit
48acf4211d
2 changed files with 69 additions and 86 deletions
5
api/.gitignore
vendored
5
api/.gitignore
vendored
|
|
@ -268,3 +268,8 @@ src/Sozsoft.Platform.Blazor.Server.Tiered/Logs/*
|
|||
# DevExpress License - DO NOT commit to repository
|
||||
**/DevExpress_License.txt
|
||||
DevExpress_License.txt
|
||||
|
||||
# Local cache files
|
||||
*.lscache
|
||||
lscache/
|
||||
**/lscache/
|
||||
|
|
@ -76,9 +76,8 @@ const _Notification = ({ className }: { className?: string }) => {
|
|||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const toastNotificationList = useRef<string[]>([])
|
||||
const toastNotificationInterval = useRef<ReturnType<typeof setInterval>>()
|
||||
const desktopNotificationList = useRef<string[]>([])
|
||||
const desktopNotificationInterval = useRef<ReturnType<typeof setInterval>>()
|
||||
const pushNotificationInterval = useRef<ReturnType<typeof setInterval>>()
|
||||
|
||||
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(
|
||||
<Notify
|
||||
type="success"
|
||||
duration={0}
|
||||
closable={true}
|
||||
>
|
||||
<Notify type="success" duration={0} closable={true}>
|
||||
{notification.message}
|
||||
</Notify>,
|
||||
{
|
||||
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)
|
||||
}
|
||||
}, [])
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue