// Nama file: firebase-messaging-sw.js

// Impor skrip inti Firebase
importScripts("https://www.gstatic.com/firebasejs/9.0.0/firebase-app-compat.js");
importScripts("https://www.gstatic.com/firebasejs/9.0.0/firebase-messaging-compat.js");

// ------------------------------------------------------------------
// PENTING: Salin-tempel 'firebaseConfig' ANDA di bawah ini
// (Blok yang sama persis seperti yang ada di home.php)
// ------------------------------------------------------------------
const firebaseConfig = {
  apiKey: "AIzaSyBRWn3rzjZaQofiyrC2JyA0ZNL0xGN3EFM",
  authDomain: "siswaplus-2b378.firebaseapp.com",
  projectId: "siswaplus-2b378",
  storageBucket: "siswaplus-2b378.firebasestorage.app",
  messagingSenderId: "836281878743",
  appId: "1:836281878743:web:0fff33a61368385698c892"
  // (Pastikan ini config Anda, bukan measurementId)
};

// Inisialisasi Firebase App di dalam Service Worker
firebase.initializeApp(firebaseConfig);

// Ambil instance Messaging
const messaging = firebase.messaging();

// (Opsional) Menangani notifikasi saat aplikasi di background
messaging.onBackgroundMessage((payload) => {
  console.log(
    "[firebase-messaging-sw.js] Menerima pesan di background: ",
    payload
  );
  
  // Tampilkan notifikasi
  const notificationTitle = payload.notification.title;
  const notificationOptions = {
    body: payload.notification.body,
    icon: '/icon-192-hijau.png' // Ganti dengan ikon Anda
  };

  self.registration.showNotification(notificationTitle, notificationOptions);
});