fix(pwa): explicitly request notification permission
This commit is contained in:
@@ -46,6 +46,21 @@ export function PushSubscriptionSettings({ planId }: { planId: string }) {
|
|||||||
async function subscribe() {
|
async function subscribe() {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
try {
|
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 registration = await navigator.serviceWorker.ready
|
||||||
const sub = await registration.pushManager.subscribe({
|
const sub = await registration.pushManager.subscribe({
|
||||||
userVisibleOnly: true,
|
userVisibleOnly: true,
|
||||||
@@ -55,8 +70,8 @@ export function PushSubscriptionSettings({ planId }: { planId: string }) {
|
|||||||
await subscribeUser(planId, sub.toJSON())
|
await subscribeUser(planId, sub.toJSON())
|
||||||
toast.success("Push Notifications enabled")
|
toast.success("Push Notifications enabled")
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error("Subscription failed", error)
|
||||||
toast.error("Failed to enable notifications. check permissions.")
|
toast.error("Failed to enable notifications. Please check site permissions.")
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user