title: How to Use Work Manager to show notifications at later time
body: <p>I'm trying to understand how to show a notification at a specific time, I understand that I can use workmanager for this purpose and that it will choose the correct method to do the job (in this case show the notification) even if the app is closed the device reset etc.</p>
  
  <p>Now I have a test method that schedules the notification and it seems that if the app is running while the job should run then everything is ok but if my app is killed before that then the notification wont show until I open the app again.</p>
  
  <p>I want to understand why wont the notification show when the app is closed and how to make work manger run the job even when the app is off.</p>
  
  <p><strong>Worker:</strong></p>
  
  <pre><code>public class WorkerNext extends Worker {
  
  public Result doWork() {
          Result workResult=Result.FAILURE;
          int workType=getInputData().getInt(DATA_KEY_WORK_TYPE,WORK_TYPE1);
          switch (workType)
          {
              case WORK_TYPE1:
                  workResult=doWorkUpdate();
                  break;
              case WORK_TYPE_SHOW_NOTIFICATION:
                  workResult=doWorkShowNotification();
                  break;
              default:
                  workResult=doWorkUpdate();
                  break;
          }
          return workResult;
      }
  
  private Result doWorkShowNotification() {
          showHagralaDetailsNotification();
          scheduleNotifications();
          return Result.SUCCESS;
      }
  
      public void showHagralaDetailsNotification()
      {
          Context context=MyApp.getApplication().getApplicationContext();
          Notification.Builder builder = new Notification.Builder(context)
                  .setContentTitle(context.getString(R.string.Notification_NextHagralaTitle))
                  .setContentText(NextHagralaDetails.toNotificationText())
                  .setAutoCancel(true)
                  .setSmallIcon(R.mipmap.ic_launcher_foreground)
                  .setLargeIcon(((BitmapDrawable) context.getResources().getDrawable(R.mipmap.ic_launcher)).getBitmap())
                  .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
  
          Notification notification = builder.getNotification();
          NotificationManager manager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
          manager.notify(NOTIFICATION_ID_HAGRALA_DETAILS,notification);
      }
  
  private void scheduleNotifications()
      {
          long delay=calculateNextNotificationsDelay();
          if (delay!=MyConfig.ALERT_NONE)
          {
              ExistingWorkPolicy workPolicy=ExistingWorkPolicy.REPLACE;
  
              Data data=new Data.Builder().putInt(WorkerNextHagrala.DATA_KEY_WORK_TYPE,
                      WorkerNextHagrala.WORK_TYPE_SHOW_NOTIFICATION).build();
              Log.d("GABI","scheduleUpdateNextNotifications - delay:"+delay);
              OneTimeWorkRequest request=new OneTimeWorkRequest.Builder(WorkerNextHagrala.class).
                      setInitialDelay(delay, TimeUnit.MILLISECONDS).setInputData(data).build();
              WorkManager.getInstance().beginUniqueWork(WORK_NAME_SHOW_NOTIFICATION,workPolicy ,request).enqueue();
          }
      }
  }
  </code></pre>
  
  <p><strong>Test method - (schedule notification 1 minute from now):</strong></p>
  
  <pre><code>public void showNotification(View view) {
          long delay=60000;
          if (delay!=MyConfig.ALERT_NONE)
          {
              ExistingWorkPolicy workPolicy=ExistingWorkPolicy.REPLACE;
  
              Data data=new Data.Builder().putInt(WorkerNextHagrala.DATA_KEY_WORK_TYPE,
                      WorkerNextHagrala.WORK_TYPE_SHOW_NOTIFICATION).build();
              Log.d("GABI","scheduleUpdateNextNotifications - delay:"+delay);
              OneTimeWorkRequest request=new OneTimeWorkRequest.Builder(WorkerNextHagrala.class).
                      setInitialDelay(delay, TimeUnit.MILLISECONDS).setInputData(data).build();
              WorkManager.getInstance().beginUniqueWork(WorkerNextHagrala.WORK_NAME_SHOW_NOTIFICATION,workPolicy ,request).enqueue();
          }
      }
  </code></pre>