package id.madrasah.absensi import android.Manifest import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.os.Build import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.core.content.ContextCompat class AttendanceReminderReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val kind = intent.getStringExtra("kind") ?: "checkin" val title = if (kind == "checkout") "Waktunya absen pulang" else "Waktunya absen masuk" val message = if (kind == "checkout") "Buka Absensi Madrasah, periksa GPS aktual, lalu konfirmasi absensi pulang." else "Buka Absensi Madrasah, periksa GPS aktual, lalu konfirmasi absensi masuk." val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { manager.createNotificationChannel( NotificationChannel("attendance_reminders", "Pengingat Absensi", NotificationManager.IMPORTANCE_HIGH) ) } val open = Intent(context, MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP putExtra("reminder_kind", kind) } val pending = PendingIntent.getActivity( context, if (kind == "checkout") 2 else 1, open, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) val notification = NotificationCompat.Builder(context, "attendance_reminders") .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle(title) .setContentText(message) .setStyle(NotificationCompat.BigTextStyle().bigText(message)) .setContentIntent(pending) .setAutoCancel(true) .setPriority(NotificationCompat.PRIORITY_HIGH) .build() val permissionGranted = Build.VERSION.SDK_INT < 33 || ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED if (permissionGranted) NotificationManagerCompat.from(context).notify(if (kind == "checkout") 2002 else 2001, notification) // Set the next weekday reminder after each trigger. ReminderScheduler.scheduleNext(context) } }