# 📱 Fix Crash APK di Android 13 Vivo

## 🔍 Penyebab Crash Ditemukan:

### 1. **Missing Permission: SCHEDULE_EXACT_ALARM** ❌ → ✅
   - **Masalah**: Background service & notification crash karena permission tidak dideklarasikan
   - **Solusi**: Tambah permission di `AndroidManifest.xml`
   ```xml
   <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
   ```

### 2. **Runtime Permission POST_NOTIFICATIONS** ❌ → ✅
   - **Masalah**: Android 13+ memerlukan runtime permission untuk POST_NOTIFICATIONS
   - **Solusi**: Tambah permission handler & request saat app launch
   ```dart
   if (Platform.isAndroid) {
     await Permission.notification.request();
     await Permission.scheduleExactAlarm.request();
   }
   ```

### 3. **Double Firebase Initialization** ❌ → ✅
   - **Masalah**: Firebase diinit di main() DAN di onStart() → memory leak & crash
   - **Solusi**: Hapus Firebase.initializeApp() di background service onStart()

### 4. **Null Safety Issue** ❌ → ✅
   - **Masalah**: Akses field tanpa null check → NullPointerException
   - **File**: `lib/data_siswa.dart`
   - **Solusi**: Cek null sebelum akses
   ```dart
   final rfidData = siswa['rfid'];
   if (rfidData == null) continue;
   final rfid = rfidData as String;
   ```

### 5. **Missing Error Handling** ❌ → ✅
   - **Masalah**: Exception di Firestore listener tidak ditangani → app crash
   - **Solusi**: Wrap dengan try-catch & onError callback
   ```dart
   .listen((event) async {
     try {
       // ...
     } catch (e) {
       debugPrint('Error notif: $e');
     }
   }, onError: (error) {
     debugPrint('Error listen absensi: $error');
   });
   ```

---

## 📝 File yang Diubah:

### ✏️ `android/app/src/main/AndroidManifest.xml`
- [x] Tambah `SCHEDULE_EXACT_ALARM` permission

### ✏️ `lib/main.dart`
- [x] Import `permission_handler` dan `dart:io`
- [x] Request permission di main()
- [x] Hapus Firebase init di background service
- [x] Tambah try-catch di Firestore listener
- [x] Tambah null safety checks untuk siswa data

### ✏️ `lib/data_siswa.dart`
- [x] Cek null sebelum akses field
- [x] Skip row jika ortu_uid kosong

### ✏️ `pubspec.yaml`
- [x] Tambah dependency: `permission_handler: ^11.4.4`

---

## 🚀 Langkah Selanjutnya:

1. **Run flutter pub get**
   ```bash
   flutter pub get
   ```

2. **Clean build**
   ```bash
   flutter clean
   flutter pub get
   ```

3. **Build APK baru**
   ```bash
   flutter build apk --release
   ```

4. **Test di Vivo Android 13**
   - Uninstall APK lama
   - Install APK baru
   - Tunggu 5-10 menit untuk background service berjalan
   - Cek logcat untuk error messages:
     ```bash
     flutter logs
     ```

---

## 🔧 Testing Background Service:

Untuk test apakah background service jalan:
1. Buka app
2. Login dengan akun parent
3. Keluar dari app
4. Tunggu 1-2 menit
5. Cek notification - seharusnya muncul "Absensi Aktif"

---

## 📊 Logcat Debug:

Jika masih ada crash, check logcat:
```bash
flutter logs | grep -E "Error|Exception|Crash"
```

Atau di Android Studio:
- Logcat → Filter: `absensi`

---

## ⚠️ Catatan Penting:

1. **Permission Request**: User harus approve notification permission saat app launch
2. **Background Service**: Tidak akan jalan jika app tidak pernah di-login
3. **Android 13+**: Foreground service notif wajib ada
4. **Debugging**: Gunakan `debugPrint()` bukan `print()` untuk log

---

## ✅ Checklist Fix:

- [x] SCHEDULE_EXACT_ALARM permission ditambah
- [x] Runtime permission request ditambah
- [x] Firebase double init dihapus
- [x] Null safety checks ditambah
- [x] Error handling ditambah
- [x] Permission_handler dependency ditambah

**Status**: Siap di-test! 🎉
