title: Sometimes alarms doesn't get triggered using AlarmManager
body: <p>I have a reminder app which notifies the user for a specific task. Everything is working as expected, but <strong>sometimes</strong> the alarm doesn't get triggered. This issue was reported by nearly 100 users. I suspect that when the devices are in sleep mode, the alarms are not triggered. I am facing this issue for nearly 1 year and I haven't found any solution yet.</p>
  
  <p>This is how the alarms are getting scheduled:</p>
  
  <pre><code>public void schedule(Context context, long nextTrigger, Intent intent) {
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQ_CODE_ALARM, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  
      int ALARM_TYPE = AlarmManager.RTC_WAKEUP;
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
          alarmManager.setExactAndAllowWhileIdle(ALARM_TYPE, nextTrigger, pendingIntent);
      }
      else if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
          alarmManager.setExact(ALARM_TYPE, nextTrigger, pendingIntent);
      }
      else {
          alarmManager.set(ALARM_TYPE, nextTrigger, pendingIntent);
      }
  }
  </code></pre>
  
  <p>This is the broadcast receiver from pending intent:</p>
  
  <pre><code>public class ReminderAlarm extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
          PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
          PowerManager.WakeLock wl = null;
          if (pm != null) {
              wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, APP_NAME);
              if (wl != null)
                  wl.acquire(10000L);
          }
  
          Log.log("Reminder alarm received");
  
          ReminderJobIntentService.enqueueWork(context, ReminderJobIntentService.class, JOB_ID_REMINDER_ALARM, intent);
  
          if (wl != null)
              wl.release();
      }
  }
  </code></pre>
  
  <p>This is the Job Intent Service which is enqueued from the broadcast:</p>
  
  <pre><code>public class ReminderJobIntentService extends JobIntentService {
  
      @Override
      protected void onHandleWork(@NonNull Intent intent) {
          Log.log("Reminder job intent service");
  
          // Creates a notification on the UI
          Utils.createNotification();
  
          // Schedule the next task (alarm). Actually it's schedule(context, long, intent) but it calculates the next trigger. That's it
          Scheduler.getInstance().scheduleNext();
      }
  }
  </code></pre>
  
  <ol>
  <li>I have logs that the alarms were scheduled correctly. When the broadcast is received (when the alarm is triggered) I'm logging this as well. When there are no logs with the broadcast, it means that the alarm doesn't triggered.</li>
  <li>The alarms are scheduled at <code>Boot Complete</code></li>
  <li>I have tried to reproduce this issue several times with different Android devices and versions but I can't reproduce it. I even tried to put the device in <code>Doze Mode</code> and <code>Battery Unpluged</code>, but no luck. The alarms were triggered. I left the device untouched for nearly 3 days, the alarms were triggered with a 4 minute delay (because of Android OS is batching the alarms).</li>
  <li>When a user is complaining about the reminders are not received, usually I redirect them to "Settings -> Battery -> Ignore battery optimization for app". But sometimes this doesn't fix the issue.</li>
  </ol>
  
  <p>What have I noticed that the people who are complaining about this are using Samsung devices: S6, S7, S8 models with Android 6, 7. (Maybe because 45% of my users are using this type of device?)</p>
  
  <p>Have you ever encountered this specific problem? Am I missing something? Are Samsung models killing the apps in the background? Maybe some 3rd party apps are killing the apps?</p>