
    (async () => {
        if (!('serviceWorker' in navigator)) { 
            console.warn('[SW] Service workers are not supported by this browser');
            window.fopPushDisabled = true;
            return;
        }

        try {
            await navigator.serviceWorker.register('serviceworker.js');
        } catch(e){
            console.error('[SW] Service worker registration failed', e);
            return;
        }

        console.log('[SW] Service worker has been registered');
        await (async () => {
            
        await (async () => {
            function urlBase64ToUint8Array(base64String) {
                const padding = '='.repeat((4 - (base64String.length % 4)) % 4);
                const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');

                const rawData = window.atob(base64);
                const outputArray = new Uint8Array(rawData.length);

                for (let i = 0; i < rawData.length; ++i) {
                    outputArray[i] = rawData.charCodeAt(i);
                }
                return outputArray;
            }

            async function checkNotificationPermission() {
                if(Notification.permission === 'denied') return false;
                if(Notification.permission === 'granted') return true;
                if(Notification.permission === 'default'){
                    let r = await Notification.requestPermission();
                    if(r !== 'granted') return false;
                    return true;
                }
            }

            if (!('PushManager' in window)) {
                console.warn('Push notifications are not supported by this browser');
                window.fopPushDisabled = true;
                return;
            }

            let pubkey = fopPushPublicKey;
            let registration = await navigator.serviceWorker.ready;
            let allowed = (await Notification.requestPermission()) === 'granted';

            if(!allowed){
                window.fopPushDisabled = true;
                return;
            }

            try {
                let subscription = await registration.pushManager.subscribe({
                    userVisibleOnly: true,
                    applicationServerKey: urlBase64ToUint8Array(pubkey),
                });

                const key = subscription.getKey('p256dh');
                const token = subscription.getKey('auth');
                
                const contentEncoding = (PushManager.supportedContentEncodings || ['aesgcm'])[0];
                const endpoint = subscription.endpoint;
                const publicKey = key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null;
                const authToken = token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null;

                console.log('Updating fop subscription');
                let r = await (await fetch('FOP/Push/registerSubscription?endpoint=' + encodeURIComponent(endpoint) + '&publicKey=' + encodeURIComponent(publicKey) + '&authToken=' + encodeURIComponent(authToken) + '&contentEncoding=' + encodeURIComponent(contentEncoding))).json();
                if(r.Status !== 'OK' && r.Status !== 'Duplicate'){
                    console.warn('Failed to update subscription');
                    return;
                }
            } catch(e){
                console.warn('Error in push configuration: ' + e);
                window.fopPushDisabled = true;
            }

            

        })();
    
        })();
    })();