push notification permissions fixed

This commit is contained in:
root 2026-02-16 13:02:46 +05:00
parent 28ec63e443
commit c94a919304
3 changed files with 37 additions and 10 deletions

View File

@ -222,6 +222,7 @@ export default function AccountSettingsPage() {
</h2> </h2>
<p className="text-sm text-slate-400 mt-1 mb-4"> <p className="text-sm text-slate-400 mt-1 mb-4">
Get notified in your browser when you receive new messages and updates. Get notified in your browser when you receive new messages and updates.
When you click Enable, your browser will show a permission prompt—choose <strong>Allow</strong>.
</p> </p>
<div className="bg-white/5 rounded-xl p-4 border border-white/10"> <div className="bg-white/5 rounded-xl p-4 border border-white/10">
{pushLoading ? ( {pushLoading ? (
@ -262,10 +263,11 @@ export default function AccountSettingsPage() {
type="button" type="button"
onClick={async () => { onClick={async () => {
setPushActionLoading(true) setPushActionLoading(true)
const ok = await subscribe() setError("")
const result = await subscribe()
setPushActionLoading(false) setPushActionLoading(false)
if (!ok) { if (!result.success) {
setError("Could not enable notifications. Check that the site has permission.") setError(result.error ?? "Could not enable notifications. Try again or check browser settings.")
} }
}} }}
disabled={pushActionLoading} disabled={pushActionLoading}

View File

@ -232,6 +232,7 @@ export default function SupplierSettingsPage() {
</h2> </h2>
<p className="text-sm text-slate-400 mb-4"> <p className="text-sm text-slate-400 mb-4">
Get notified in your browser when you receive new messages and updates. Get notified in your browser when you receive new messages and updates.
When you click Enable, your browser will show a permission prompt—choose <strong>Allow</strong>.
</p> </p>
<div className="bg-white/5 rounded-xl p-4 border border-white/10"> <div className="bg-white/5 rounded-xl p-4 border border-white/10">
{pushLoading ? ( {pushLoading ? (
@ -272,9 +273,10 @@ export default function SupplierSettingsPage() {
type="button" type="button"
onClick={async () => { onClick={async () => {
setPushActionLoading(true) setPushActionLoading(true)
const ok = await subscribe() setError("")
const result = await subscribe()
setPushActionLoading(false) setPushActionLoading(false)
if (!ok) setError("Could not enable notifications. Check that the site has permission.") if (!result.success) setError(result.error ?? "Could not enable notifications. Try again or check browser settings.")
}} }}
disabled={pushActionLoading} disabled={pushActionLoading}
className="px-4 py-2.5 bg-indigo-500 text-white rounded-xl font-bold hover:bg-indigo-600 transition-colors flex items-center gap-2 disabled:opacity-50" className="px-4 py-2.5 bg-indigo-500 text-white rounded-xl font-bold hover:bg-indigo-600 transition-colors flex items-center gap-2 disabled:opacity-50"

View File

@ -57,10 +57,17 @@ export function usePushNotifications() {
checkSubscription() checkSubscription()
}, [checkSubscription]) }, [checkSubscription])
const subscribe = async () => { const subscribe = async (): Promise<{ success: boolean; error?: string }> => {
if (!VAPID_PUBLIC_KEY) { if (!VAPID_PUBLIC_KEY) {
console.error('VAPID public key not found') console.error('VAPID public key not found')
return false return { success: false, error: 'Server configuration error. Please try again later or contact support.' }
}
if (Notification.permission === 'denied') {
return {
success: false,
error: 'Notifications are blocked. In your browser, click the lock icon in the address bar → Site settings → Notifications → set to Allow, then refresh this page.'
}
} }
try { try {
@ -79,12 +86,28 @@ export function usePushNotifications() {
if (response.ok) { if (response.ok) {
setIsSubscribed(true) setIsSubscribed(true)
setPermission(Notification.permission) setPermission(Notification.permission)
return true return { success: true }
} }
return false
const data = await response.json().catch(() => ({}))
const msg = data?.message || (response.status === 401 ? 'Please log in again.' : 'Server error. Please try again later.')
return { success: false, error: msg }
} catch (error) { } catch (error) {
console.error('Error subscribing to push notifications:', error) console.error('Error subscribing to push notifications:', error)
return false const isPermissionDenied = error instanceof Error && (
error.name === 'NotAllowedError' ||
(error as DOMException).name === 'NotAllowedError'
)
if (isPermissionDenied) {
return {
success: false,
error: 'Notification permission was denied. To enable: click the lock icon in the address bar → Site settings → Notifications → Allow, then refresh.'
}
}
return {
success: false,
error: 'Could not enable notifications. Make sure the site is allowed to show notifications in your browser settings, then try again.'
}
} }
} }