fix(pwa): explicitly request notification permission
This commit is contained in:
@@ -46,6 +46,21 @@ export function PushSubscriptionSettings({ planId }: { planId: string }) {
|
||||
async function subscribe() {
|
||||
setLoading(true)
|
||||
try {
|
||||
// Explicitly request permission first
|
||||
const permission = await Notification.requestPermission()
|
||||
|
||||
if (permission === 'denied') {
|
||||
toast.error("Notifications are blocked in your browser settings.")
|
||||
setLoading(false)
|
||||
return
|
||||
}
|
||||
|
||||
if (permission !== 'granted') {
|
||||
toast.error("Notifications permission not granted.")
|
||||
setLoading(false)
|
||||
return
|
||||
}
|
||||
|
||||
const registration = await navigator.serviceWorker.ready
|
||||
const sub = await registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
@@ -55,8 +70,8 @@ export function PushSubscriptionSettings({ planId }: { planId: string }) {
|
||||
await subscribeUser(planId, sub.toJSON())
|
||||
toast.success("Push Notifications enabled")
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
toast.error("Failed to enable notifications. check permissions.")
|
||||
console.error("Subscription failed", error)
|
||||
toast.error("Failed to enable notifications. Please check site permissions.")
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user