diff options
| author | Fuwn <[email protected]> | 2024-07-24 21:24:09 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-07-24 21:24:09 -0700 |
| commit | 8a94bbfba322f8011017980e4362d46c4d51bb55 (patch) | |
| tree | 4d861cd7f1d9304e6297e1b52fe759674fd5df1a /src/lib/Utility | |
| parent | feat(layout): browser notifications (diff) | |
| download | due.moe-8a94bbfba322f8011017980e4362d46c4d51bb55.tar.xz due.moe-8a94bbfba322f8011017980e4362d46c4d51bb55.zip | |
feat: background notifications
Diffstat (limited to 'src/lib/Utility')
| -rw-r--r-- | src/lib/Utility/notifications.ts | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/lib/Utility/notifications.ts b/src/lib/Utility/notifications.ts new file mode 100644 index 00000000..fe174bd6 --- /dev/null +++ b/src/lib/Utility/notifications.ts @@ -0,0 +1,22 @@ +import { env } from '$env/dynamic/public'; +import root from './root'; + +export const requestNotifications = async () => { + if ('Notification' in window && navigator.serviceWorker) + if ((await Notification.requestPermission()) === 'granted') { + const pushSubscription = await ( + await navigator.serviceWorker.ready + ).pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: env.PUBLIC_VAPID_PUBLIC_KEY + }); + + await fetch(root('/api/notifications/subscribe'), { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(pushSubscription) + }); + } +}; |