Frank-py
Frank-py

Reputation: 3

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" />

Upvotes: 0

Views: 518

Answers (3)

Frank-py
Frank-py

Reputation: 3

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.

Upvotes: 0

Perry
Perry

Reputation: 133

First you need to update permissions - SCHEDULE_EXACT_ALARM & USE_EXACT_ALARM don't work together. One is up to SDK32 and the other on 33+

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" android:maxSdkVersion="32" /> <uses-permission android:name="android.permission.USE_EXACT_ALARM" />

Then you should check multiple battery saving features that might block alarms in your app.

  • is app ignoring battery optimizations?
  • is schedule exact alarms permission granted (maybe not needed if you use setAlarmClock() instead of setExactAndAllowWhileIdle() )?
  • is app hibernation restriction disabled?

Unfortunately playing with exact alarms in Android is painful.

Upvotes: 0

hi.cosmonaut
hi.cosmonaut

Reputation: 163

I had similar problem and I have check list for you)

  1. add set(Calendar.SECONDS, 0) because it can be in range of 0..59 when you set alarm.
  2. change your pendingIntent with val pendingIntent = PendingIntent.getBroadcast(context, reqCode, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

P.S you can check my project with alarms https://github.com/hicosmonaut/hourly/tree/master nothing special, app alarms every hour like Casio watch hourly alarm. Maybe you find some useful for you there are PendingIntent, AlarmClock etc

Upvotes: 0

Related Questions