GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/html/resources/views/buyer/chat.blade.php
@extends('layouts.app')
@section('title', 'My Chats')
@section('app')
@livewire('buyer.chat')
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script type="module">
    // Import the functions you need from the SDKs you need
    import {
        initializeApp
    } from "https://www.gstatic.com/firebasejs/11.1.0/firebase-app.js";
    import {
        getAnalytics
    } from "https://www.gstatic.com/firebasejs/11.1.0/firebase-analytics.js";
    import {
        getMessaging,
        getToken,
        onMessage
    } from "https://www.gstatic.com/firebasejs/11.1.0/firebase-messaging.js";


    // Your web app's Firebase configuration
    const firebaseConfig = {
        apiKey: "AIzaSyBttDksOxuahV-SCex9ho2SpHrYeSbwnj4",
        authDomain: "fixgini.firebaseapp.com",
        projectId: "fixgini",
        storageBucket: "fixgini.firebasestorage.app",
        messagingSenderId: "994362424638",
        appId: "1:994362424638:web:9b0a92ba6c61ecabd6006f",
        measurementId: "G-ECEEH8CQVH"
    };



    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
    const analytics = getAnalytics(app);
    // const messaging = firebase.messaging();
    const messaging = getMessaging(app);

    // console.log('log payload ', messaging);

    // Register service worker
    navigator.serviceWorker.register("https://fixgini.com/sw.js")
        .then(registration => {

            // Get FCM token
            getToken(messaging, {
                serviceWorkerRegistration: registration,
                vapidKey: 'BBlAzMnqfiuHdeF0T8YQM6D-wjDGFZcZlZLtNbKE2jxc7MgXSgd8ZdKQ0U12-jrhQh2o1lpwG8sLvdYlOE-F-Zg',
            }).then((currentToken) => {
                if (currentToken) {
                    // console.log('Token gotten:', currentToken);
                    // Send the token to your server and update the UI if necessary
                    sendTokenToServer(currentToken);

                } else {
                    console.log('No registration token available. Request permission to generate one.');
                }
            }).catch((err) => {
                console.log('An error occurred while retrieving token: ', err);
            });
        }).catch(error => {
            console.error('Error registering service worker: ', error);
        });
    // Function to send token to the backend API
    function sendTokenToServer(token) {
        // Replace with the correct API endpoint
        const apiUrl = 'https://console.fixgini.com/api/v1/save-fcm-token';

        // Use fetch to send the token
        fetch(apiUrl, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer ' + getBearerToken(), // Add the Bearer token for authentication
                },
                body: JSON.stringify({
                    token: token
                }), // Send the token in the request body
            })
            .then(response => response.json())
            .then(data => {
                // console.log('Token sent successfully:', data);
            })
            .catch(error => {
                console.error('Error sending token to the server:', error);
            });
    }
    // Function to get the Bearer token (replace this with your logic to get the user token)
    function getBearerToken() {
        // You can replace this logic with your own logic to get the Bearer token
        return '{{$userBearToken}}'; // Placeholder for actual Bearer token
    }
    // Handle foreground messages
    onMessage(messaging, (payload) => {
        console.log('[Firebase Messaging] Received foreground message:', payload);

        const notificationTitle = payload.notification.title;
        const notificationOptions = {
            body: payload.notification.body,
            icon: payload.notification.icon || '/icon-512x512.png',
        };

        new Notification(notificationTitle, notificationOptions);
    });
</script>
@endsection