self.addEventListener('push',e=>{
const d=e.data?.json()||{};
e.waitUntil(self.registration.showNotification(d.title||'',{body:d.body||'',icon:'/img/icon.png',badge:'/img/icon.png',data:{url:d.url?.startsWith('/')?d.url:'/'}}))
});

self.addEventListener('notificationclick',e=>{
e.notification.close();
const u=e.notification.data.url;
e.waitUntil(clients.matchAll({type:'window',includeUncontrolled:true}).then(cs=>cs.find(c=>c.url.includes(u)&&c.focus)?.focus()||clients.openWindow?.(u)))
});
