androidkotlinalarmmanageralarmandroid-alarms

Alarm not displaying a notification or playing a sound, even though it is set


I'm currently working on an Android app that sets an alarm to trigger at a specific time. I've implemented the code to set the alarm, and it seems to get set without any errors, which can be seen here and in the alarm tab.

However, the alarm doesn't trigger at the designated time, and as a result, there's no notification, sound, or any other action referenced in setAlarmClock played (apart from the pending intent, which is being started correctly).

Here's a simplified version of the code I'm using to set the alarm:

val intent = Intent(context, AlarmClockReceiver::class.java)

val pendingIntent = PendingIntent.getBroadcast(context, reqCode, intent, PendingIntent.FLAG_IMMUTABLE)

val alarmManager: AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager


val calendar: Calendar = Calendar.getInstance().apply {
    timeInMillis = System.currentTimeMillis()
    set(Calendar.HOUR_OF_DAY, desiredHour)
    set(Calendar.MINUTE, desiredMinute)
val calendarMillis = calendar.timeInMillis
alarmManager.setAlarmClock(AlarmManager.AlarmClockInfo(calendarMillis, pendingIntent), pendingIntent)

I have checked if I can set exact Alarms and I've defined and requested the following permissions:

    <uses-permission android:name="android.permission.USE_EXACT_ALARM"/>
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

Solution

  • It seems like setAlarmClock doesn't implement default behavior with notifications and sound. I just think the documentation could express it a bit more clearly.