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 { try {
const loc = getCurrentLocation() const loc = getCurrentLocation()
const registration = await navigator.serviceWorker.register('/serviceWorker.js', { 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() const current = await registration.pushManager.getSubscription()
if (current == null) { if (current == null) {

View File

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