Avatar ·

How to properly configure AlarmManager in Android to execute a task at exact time even if the app is closed? Example in Kotlin

Hi everyone! I'm a beginner in Android development with Kotlin and I can't figure out AlarmManager. I need the app to send a notification every day at 9:00 AM, even if it's completely closed (swiped away from the recent apps list).\n\nI wrote this code, but it doesn't work when the app is killed:\n\n```kotlin\nval alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager\nval intent = Intent(this, MyReceiver::class.java)\nval pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)\n\nval calendar = Calendar.getInstance().apply {\n set(Calendar.HOUR_OF_DAY, 9)\n set(Calendar.MINUTE, 0)\n set(Calendar.SECOND, 0)\n}\n\nalarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, AlarmManager.INTERVAL_DAY, pe