Fix service worker scope in BrowserNotificatator (#5391)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2024-04-17 23:11:52 +05:00 committed by GitHub
parent a29e1701be
commit e16172086c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 8 deletions

View File

@ -28,7 +28,7 @@
try {
const loc = getCurrentLocation()
const registration = await navigator.serviceWorker.register('/serviceWorker.js', {
scope: `./${loc.path[0]}/${loc.path[1]}`
scope: `${loc.path[0]}/${loc.path[1]}`
})
const current = await registration.pushManager.getSubscription()
if (current == null) {

View File

@ -41,7 +41,14 @@ import notification, {
type MentionInboxNotification
} from '@hcengineering/notification'
import { MessageBox, getClient } from '@hcengineering/presentation'
import { getLocation, navigate, showPopup, type Location, type ResolvedLocation } from '@hcengineering/ui'
import {
getCurrentLocation,
getLocation,
navigate,
showPopup,
type Location,
type ResolvedLocation
} from '@hcengineering/ui'
import { get } from 'svelte/store'
import { InboxNotificationsClientImpl } from './inboxNotificationsClient'
@ -535,12 +542,16 @@ export function openInboxDoc (
export async function checkPermission (value: boolean): Promise<boolean> {
if (!value) return true
if ('Notification' in window) {
if (Notification?.permission === 'denied') return false
if (Notification?.permission === 'granted') return true
if (Notification?.permission === 'default') {
const res = await Notification?.requestPermission()
return res === 'granted'
if ('serviceWorker' in navigator && 'PushManager' in window) {
try {
const loc = getCurrentLocation()
const registration = await navigator.serviceWorker.getRegistration(`/${loc.path[0]}/${loc.path[1]}`)
if (registration !== undefined) {
const current = await registration.pushManager.getSubscription()
return current !== null
}
} catch {
return false
}
}
return false