diff --git a/server/public/home.html b/server/public/home.html index f53bf29..990bf4a 100644 --- a/server/public/home.html +++ b/server/public/home.html @@ -589,7 +589,7 @@ - +
Loading…
@@ -1453,20 +1453,25 @@ document.addEventListener('click', ensureNotifyPermission, { once:true }); let pushActive=false, _swReg=null; function urlB64ToUint8(base64){ const pad='='.repeat((4-base64.length%4)%4); const b=(base64+pad).replace(/-/g,'+').replace(/_/g,'/'); const raw=atob(b); const arr=new Uint8Array(raw.length); for(let i=0;i{ const d=e.data||{}; if(d.type==='open-chat' && d.id){ try{ selectChat(d.kind||'dm', d.id); }catch(_){} } }); }catch(_){} - try{ await subscribePush(); }catch(_){} + await subscribePush(); } async function subscribePush(){ - if(!_swReg || !('PushManager' in window)) return; - if(!('Notification' in window) || Notification.permission!=='granted') return; // only once allowed - let cfg; try{ cfg=await fetch('/api/push/vapid').then(r=>r.json()); }catch(_){ return; } - if(!cfg || !cfg.enabled || !cfg.key) return; // server hasn't configured VAPID -> stay on in-page notifs - let sub=null; try{ sub=await _swReg.pushManager.getSubscription(); }catch(_){} - if(!sub){ try{ sub=await _swReg.pushManager.subscribe({ userVisibleOnly:true, applicationServerKey:urlB64ToUint8(cfg.key) }); }catch(_){ return; } } - try{ await postJSON('/api/push/subscribe', sub.toJSON()); pushActive=true; }catch(_){} + try{ + if(!_swReg){ try{ _swReg=await navigator.serviceWorker.ready; }catch(_){ return; } } + if(!('Notification' in window) || Notification.permission!=='granted'){ console.log('[push] permission not granted ('+(window.Notification?Notification.permission:'n/a')+') — skipping subscribe'); return; } + let cfg; try{ cfg=await fetch('/api/push/vapid').then(r=>r.json()); }catch(e){ console.warn('[push] /api/push/vapid failed:', e); return; } + if(!cfg || !cfg.enabled || !cfg.key){ console.warn('[push] server push disabled (VAPID not set):', cfg); return; } + let sub=await _swReg.pushManager.getSubscription(); + if(!sub){ sub=await _swReg.pushManager.subscribe({ userVisibleOnly:true, applicationServerKey:urlB64ToUint8(cfg.key) }); } + await postJSON('/api/push/subscribe', sub.toJSON()); + pushActive=true; console.log('[push] subscribed OK'); + }catch(e){ console.warn('[push] subscribe failed:', e); } // surfaced (not swallowed) so we can see the real reason } // Open the chat from an in-page notification. Navigation reliably repaints across browsers (a // notification click is not an in-page gesture, so an in-place open won't paint until you