Notification düzenlemesi
This commit is contained in:
parent
f62d4b1a74
commit
48acf4211d
2 changed files with 69 additions and 86 deletions
7
api/.gitignore
vendored
7
api/.gitignore
vendored
|
|
@ -267,4 +267,9 @@ src/Sozsoft.Platform.Blazor.Server.Tiered/Logs/*
|
||||||
|
|
||||||
# DevExpress License - DO NOT commit to repository
|
# DevExpress License - DO NOT commit to repository
|
||||||
**/DevExpress_License.txt
|
**/DevExpress_License.txt
|
||||||
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 [loading, setLoading] = useState(false)
|
||||||
|
|
||||||
const toastNotificationList = useRef<string[]>([])
|
const toastNotificationList = useRef<string[]>([])
|
||||||
const toastNotificationInterval = useRef<ReturnType<typeof setInterval>>()
|
|
||||||
const desktopNotificationList = useRef<string[]>([])
|
const desktopNotificationList = useRef<string[]>([])
|
||||||
const desktopNotificationInterval = useRef<ReturnType<typeof setInterval>>()
|
const pushNotificationInterval = useRef<ReturnType<typeof setInterval>>()
|
||||||
|
|
||||||
const { bgTheme } = useThemeClass()
|
const { bgTheme } = useThemeClass()
|
||||||
const { larger } = useResponsive()
|
const { larger } = useResponsive()
|
||||||
|
|
@ -103,7 +102,9 @@ const _Notification = ({ className }: { className?: string }) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getReactNotificationCount()
|
getReactNotificationCount()
|
||||||
var intervalId = setInterval(() => {
|
var intervalId = setInterval(() => {
|
||||||
getReactNotificationCount()
|
if (tabHasFocusRef.current) {
|
||||||
|
getReactNotificationCount()
|
||||||
|
}
|
||||||
}, notificationInterval)
|
}, notificationInterval)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
|
@ -174,114 +175,91 @@ const _Notification = ({ className }: { className?: string }) => {
|
||||||
[notificationList],
|
[notificationList],
|
||||||
)
|
)
|
||||||
|
|
||||||
// Toast
|
const getPushNotifications = async () => {
|
||||||
const getToastNotifications = async () => {
|
const desktopGranted =
|
||||||
|
'Notification' in window && window.Notification.permission === 'granted'
|
||||||
|
|
||||||
|
const channels = [NotificationChannels.UiToast]
|
||||||
|
if (desktopGranted) channels.push(NotificationChannels.Desktop)
|
||||||
|
|
||||||
const resp = await getList({
|
const resp = await getList({
|
||||||
channels: [NotificationChannels.UiToast],
|
channels,
|
||||||
isListRequest: false,
|
isListRequest: false,
|
||||||
isSent: false,
|
isSent: false,
|
||||||
maxResultCount: 1000,
|
maxResultCount: 1000,
|
||||||
})
|
})
|
||||||
const items = resp.data.items ?? []
|
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 = [
|
||||||
...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(
|
toast.push(
|
||||||
<Notify
|
<Notify type="success" duration={0} closable={true}>
|
||||||
type="success"
|
|
||||||
duration={0}
|
|
||||||
closable={true}
|
|
||||||
>
|
|
||||||
{notification.message}
|
{notification.message}
|
||||||
</Notify>,
|
</Notify>,
|
||||||
{
|
{ placement: 'bottom-end' },
|
||||||
placement: 'bottom-end',
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
await updateSent(notification.id, true)
|
await updateSent(notification.id, true)
|
||||||
await updateRead(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(() => {
|
useEffect(() => {
|
||||||
if (tabHasFocusRef.current) {
|
if ('Notification' in window && window.Notification.permission === 'default') {
|
||||||
getToastNotifications()
|
window.Notification.requestPermission()
|
||||||
}
|
}
|
||||||
toastNotificationInterval.current = setInterval(async () => {
|
|
||||||
|
getPushNotifications()
|
||||||
|
|
||||||
|
pushNotificationInterval.current = setInterval(() => {
|
||||||
if (tabHasFocusRef.current) {
|
if (tabHasFocusRef.current) {
|
||||||
await getToastNotifications()
|
getPushNotifications()
|
||||||
}
|
}
|
||||||
}, notificationInterval)
|
}, notificationInterval)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
clearInterval(toastNotificationInterval.current)
|
clearInterval(pushNotificationInterval.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)
|
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue