title: Repeating a notification every day when app dies
body: <p>I need to know how to do a notification every day.<br>
  I tried with AlarmManager, but when app close, AlarmManager doesn't work.<br>
  I tried with Service but do the same, maybe i don't have the service correctly.</p>
  
  <p>I'm trying in Xiaomi Mi6, maybe service die when batery saving turns on.<br>
  I need notification activate every day at 8.00 AM.</p>
  
  <pre><code>public class NotificationService extends Service {
  
  final Handler handler = new Handler();
  
  
  @Override
  public IBinder onBind(Intent arg0) {
      return null;
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      super.onStartCommand(intent, flags, startId);
      startTimer(NotificationService.this);
      return START_STICKY;
  }
  
  public void startTimer(final NotificationService notificationService) {
      final Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(System.currentTimeMillis());
      if (calendar.get(Calendar.HOUR_OF_DAY) &gt;= 8) {
          calendar.add(Calendar.DATE, 1);
      }
  
      calendar.set(Calendar.HOUR_OF_DAY, 8);
      calendar.set(Calendar.MINUTE, 0);
      calendar.set(Calendar.SECOND, 0);
  
  
      long delay = calendar.getTimeInMillis() - System.currentTimeMillis();
      Runnable runnable = new Runnable() {
          @Override
          public void run() {
              notification(notificationService);
              handler.postDelayed(this, 86400000); // 24 
          }
      };
  
      if(delay &gt; 0) {
          handler.postDelayed(runnable, delay);
      }
  }
  
  public void notification(Context ctx) {
      NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
  
      Intent notificationIntent = new Intent(ctx, Splash.class); pulsar en la notificación
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
      PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0,
              notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  
      Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  
      String[] frasesPosibles = ctx.getResources().getStringArray(R.array.notificacionFrase);
      int rnd = new Random().nextInt(frasesPosibles.length);
      String frase = frasesPosibles[rnd];
  
      NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
              ctx).setSmallIcon(R.mipmap.ic_launcher)
              .setContentTitle(ctx.getResources().getString(R.string.newPhraseTitle))
              .setContentText(frase).setSound(alarmSound)
              .setAutoCancel(true).setWhen(System.currentTimeMillis())
              .setContentIntent(pendingIntent)
              .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
      notificationManager.notify(0, mNotifyBuilder.build());
  }
  
  @Override
  public void onCreate() {
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
  }
  
  @Override
  public void onTaskRemoved(Intent rootIntent) {
      Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
      restartServiceIntent.setPackage(getPackageName());
  
      PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
      AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
      alarmService.set(AlarmManager.ELAPSED_REALTIME,
              SystemClock.elapsedRealtime() + 1000,
              restartServicePendingIntent);
  
      super.onTaskRemoved(rootIntent);
  }
  </code></pre>