<script>
    if ('serviceWorker' in navigator) {
        navigator.serviceWorker.register('../firebase-messaging-sw.js')
            .then(registration => {
                //   console.log('Service worker registered:', registration);
            })
            .catch(error => {
                console.error('Service worker registration failed:', error);
            });
    }
</script>


<script src="https://www.gstatic.com/firebasejs/8.3.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.3.2/firebase-messaging.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

<script type="module">
    // Import the functions you need from the SDKs you need
    // import { initializeApp } from "https://www.gstatic.com/firebasejs/10.13.2/firebase-app.js";
    // import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.13.2/firebase-analytics.js";
    // TODO: Add SDKs for Firebase products that you want to use
    // https://firebase.google.com/docs/web/setup#available-libraries

    // Your web app's Firebase configuration
    // For Firebase JS SDK v7.20.0 and later, measurementId is optional
    const firebaseConfig = {
        apiKey: "AIzaSyB-C57QtBHq8Sm4CEE7CmPR9HZZc1lLcUY",
        authDomain: "trading-fmcg-292b2.firebaseapp.com",
        projectId: "trading-fmcg-292b2",
        storageBucket: "trading-fmcg-292b2.firebasestorage.app",
        messagingSenderId: "209774616786",
        appId: "1:209774616786:web:48c52b1e14f8c57f234d0b"
    };

    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    const messaging = firebase.messaging();

    function initFirebaseMessagingRegistration() {
        // console.log("initFirebaseMessagingRegistration")
        messaging.requestPermission().then(function() {
            return messaging.getToken()
        }).then(function(token) {

            // Store in localStorage
            localStorage.setItem('fcm_token', token);
            axios.post("<?php echo e($route); ?>", {
                _method: "PATCH",
                token
            }).then(({
                data
            }) => {
                console.log(data)
            }).catch(({
                response: {
                    data
                }
            }) => {
                console.error(data)
            })

        }).catch(function(err) {
            console.log(`Token Error :: ${err}`);
        });
    }

    initFirebaseMessagingRegistration();

    messaging.onMessage((payload) => {
        console.log("onMessage Receive", payload);
        //alert(`Notification: ${payload.notification.title} - ${payload.notification.body}`);
        new Notification(payload.notification.title, {
            body: payload.notification.body
        });
        if (payload.data.notif_type != 'MESSAGE') {
            $("#dotNotify").css('display', 'block')
        } else if (payload.data.notif_type == 'MESSAGE') {
            $("#dotMessages").css('display', 'block')
        }
    });
</script>
<?php /**PATH E:\laragon\www\medibill\resources\views/push-notification.blade.php ENDPATH**/ ?>