Files
binome/src/utils/notification.ts

57 lines
1.2 KiB
TypeScript

declare const Notification: any
let showNotification:
| ((
title: string,
options?: NotificationOptions | undefined
) => Promise<void>)
| null = null
export const setNotificationInstance = (
sn: (
title: string,
options?: NotificationOptions | undefined
) => Promise<void>
) => {
showNotification = sn
}
export const notify = (
title: string,
options?: NotificationOptions | undefined
) => {
const showCustomNotification = (
title: string,
options?: NotificationOptions | undefined
) => {
if (!showNotification) {
return
}
showNotification(title, {
icon: '/img/icons/apple-touch-icon.png',
vibrate: [300, 100, 400],
...options
})
}
try {
if (!('Notification' in window)) {
return
} else if (Notification.permission === 'granted') {
showCustomNotification(title, options)
} else if (Notification.permission !== 'denied') {
Notification.requestPermission((permission: any) => {
if (!('permission' in Notification)) {
Notification.permission = permission
}
if (permission === 'granted') {
showCustomNotification(title, options)
}
})
}
} catch (error) {
return
}
}