(function(){ 'use strict'; if (window.__MB_OS_INIT__) { return; } window.__MB_OS_INIT__ = true; var APP_ID = "415d9686-ea39-45cf-b641-252e38ae95bb"; var KEY_PROMPTED = 'pwa_notif_prompted_final'; var isStandalone = (function(){ try { return (window.matchMedia && window.matchMedia('(display-mode: standalone)').matches) || window.navigator.standalone === true; } catch(e){ return false; } })(); function loadSDK(cb){ if (window.OneSignal && window.OneSignal.Notifications) { cb(); return; } if (document.querySelector('script[src*="OneSignalSDK.page.js"]')) { var tries=0;(function wait(){ if (window.OneSignal && window.OneSignal.Notifications) { cb(); return; } if (tries++>40) { cb(); return; } setTimeout(wait,150); })(); return; } var s=document.createElement('script'); s.src='https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js'; s.defer=true; s.onload=cb; document.head.appendChild(s); } function sendPlayerId(){ if (!window.OneSignal || !OneSignal.User || !OneSignal.User.getId) return; OneSignal.User.getId().then(function(id){ if (!id) return; try{ var url='/pwa/onesignal_register.php' + '?uid=' + encodeURIComponent(localStorage.getItem('pwa_uid')||'') + '&playerId=' + encodeURIComponent(id) + '&t=' + Date.now(); new Image().src=url; // GET beacon → aucun JSON dans le JS }catch(e){} }); } function showCTA(){ // iOS exige un geste utilisateur dans la PWA installée if (!isStandalone) return; if (Notification && Notification.permission==='granted') return; if (localStorage.getItem(KEY_PROMPTED)==='1') return; if (!document.getElementById('mb-cta-style')){ var css='#mb-cta{position:fixed;left:12px;right:12px;bottom:12px;z-index:99999;background:#0b1220;color:#fff;border:1px solid #213050;border-radius:14px;box-shadow:0 8px 30px rgba(0,0,0,.35);padding:14px 16px;font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial,sans-serif}' +'#mb-cta h4{margin:0 0 6px;font-size:16px}#mb-cta p{margin:0 0 10px;opacity:.95}' +'#mb-cta .row{display:flex;gap:10px}#mb-cta button{flex:1;padding:10px 14px;border-radius:10px;border:0;cursor:pointer;font-weight:700}' +'#mb-cta .ok{background:#ffd54d;color:#0b1220}#mb-cta .later{background:#142038;color:#fff;border:1px solid #263253}'; var st=document.createElement('style'); st.id='mb-cta-style'; st.textContent=css; (document.head||document.body).appendChild(st); } if (document.getElementById('mb-cta')) return; var el=document.createElement('div'); el.id='mb-cta'; el.innerHTML='
Reçois les alertes vigilance, les prévisions et les infos météo importantes.
' +''; document.body.appendChild(el); document.getElementById('mb-cta-later').addEventListener('click', function(){ localStorage.setItem(KEY_PROMPTED,'1'); el.remove(); }); document.getElementById('mb-cta-ok').addEventListener('click', function(){ if (window.OneSignal && OneSignal.Notifications && OneSignal.Notifications.requestPermission){ OneSignal.Notifications.requestPermission().then(function(){ localStorage.setItem(KEY_PROMPTED,'1'); el.remove(); sendPlayerId(); }).catch(function(){ localStorage.setItem(KEY_PROMPTED,'1'); el.remove(); }); } else { setTimeout(showCTA, 600); } }); } window.MBNotif = { reprompt: function(){ localStorage.removeItem(KEY_PROMPTED); showCTA(); } }; loadSDK(function(){ window.OneSignalDeferred = window.OneSignalDeferred || []; OneSignalDeferred.push(function(OneSignal){ try{ OneSignal.init({ appId: APP_ID, serviceWorkerParam: { scope: "/" }, serviceWorkerPath: "/sw.js", serviceWorkerUpdaterPath: "/sw.js", promptOptions: { slidedown: { enabled:false, autoPrompt:false } } }); }catch(e){ console.warn('[PWA] OneSignal.init', e); } try{ OneSignal.User.PushSubscription.addEventListener('change', function(ev){ if (ev.current && ev.current.optedIn) sendPlayerId(); }); setTimeout(sendPlayerId, 800); }catch(e){} setTimeout(showCTA, 700); }); }); })();