How to Make Repeating Alarm works even if device is in Doze mode

698 Views Asked by At

I've searched around but did not found the solution. My application is required to show daily notifications on different times decided by user but alarmmanager is not working properly, If I set time for next few minute it will show notification but most of the time when I'm not using device it do not works. this is what i've been trying to set repeating alarm.

        Intent intent = new Intent(Reminder.this, AlarmReceiverDaily.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(Reminder.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) Reminder.this.getSystemService(ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);

How I can set repeating alarm even if my device is in Doze mode from the following code?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(targetCal.getTimeInMillis(),pendingIntent),pendingIntent);
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        alarmManager.setExact(AlarmManager.RTC, targetCal.getTimeInMillis(), pendingIntent);
    else
        alarmManager.set(AlarmManager.RTC, targetCal.getTimeInMillis(), pendingIntent);

If I can not use setAlarmClock() with repeating alarm then what solution you prefer to make repeating alarm and it must work.

1

There are 1 best solutions below

2
On

You have to use:

if( Build.VERSION.SDK_INT >= 23 ){
    alarmManager.setExactAndAllowWhileIdle( ... );
}