title: Android Notification doesn't work when screen is off
body: <p>The application is pretty simple. I have a regular app with all the activities etc... </p>
  
  <p>I have a service that launches the first time the application starts and/or on device reboot.</p>
  
  <p>I have a BroadcastReceiver set up to handle the notification intent and it's pretty basic as well and functions perfectly.</p>
  
  <p>The notification works perfect when the display on the android device is on. But when I turn the screen off, the notifications are no longer created.</p>
  
  <p>I am new to notifications so it's probably something simple I've overlooked, but I can't seem to find it.</p>
  
  <p>In my service I construct a worker thread which contains a Timer which fires once a minute and takes a TimerTask as part of the schedule method:</p>
  
  <pre><code>
  @Override
  public int onStartCommand(Intent intent, int flags, int startId)
  {
      boolean isStopping = false;
      if (intent != null)  // if the system has to shut us down, go down gracefully
      {
          String intentAction = intent.getStringExtra(AppData.BROADCAST_ACTION);
          if (intentAction != null && intentAction.equals("android.intent.action.REBOOT"))
          {
              boolean isRebooting = intent.getBooleanExtra(AppData.SYSTEM_ACTION, false);
              if (isRebooting)
              {
                  isStopping = true;
                  stopSelf();
              }
          }
      }
  
      if (!isStopping && !_workerThread.isRunning())
      {
          _workerThread.run();
      }
  
      return Service.START_STICKY;
  }
  
  _timer.schedule(_service.getWorker(), startDelay, executionDelay);
  </code></pre>
  
  <p>The NotificationTask (TimerTask) ultimately gets fired every minute from the Timer. It then  checks some logic to see if any of the notifications need to be created, if so, it creates and sends the notifications like this:</p>
  
  <pre><code>
  Notification notification = new NotificationCompat.Builder(_context)
          .setContentTitle(getString(R.string.notification))
          .setContentText(message)
          .setLargeIcon(decodeResource(_context.getResources(), iconId))
          .setSmallIcon(R.drawable.logo_mark)
          .setTicker(ticker)
          .setAutoCancel(true)
          .setSound(getRingtone(ringtoneUrl))
          .setVibrate(VIBRATION_PATTERN)
          .setLights(0xfff89829, 300, 5000)
          .setContentIntent(intent)
          .build();
  
  _notificationManager.notify(notificationId, notification);
  </code></pre>
  
  <p>The notifications are unique by the types of notifications and I don't care if the user has responded to one before I overwrite with the next.  My problem is I don't get the notifications if the screen is off.</p>
  
  <p>Please help.</p>
  
  <hr>
  
  <h2>UPDATE</h2>
  
  <p>I wanted to add code in case someone gets as stuck as I did.</p>
  
  <p>Let's start with the AndroidManifest. Include 2 BroadcastReceivers and your service</p>
  
  <pre><code>
  &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/&gt;
  
  &lt;service android:name=".services.NotificationService"/&gt;
  
  &lt;receiver android:name=".receivers.BootReceiver"&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.BOOT_COMPLETED"/&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  &lt;receiver android:name=".receivers.NotificationReceiver" /&gt;
  </code></pre>
  
  <p>The boot receiver is just going to schedule your Alarm when your device gets rebooted. You set it up to call your other receiver like so:</p>
  
  <pre><code>
  AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, NotificationReceiver.class), 0);
  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance(), 60 * 1000, pendingIntent);
  </code></pre>
  
  <p>The main receiver is going to launch your service - your service should extend the IntentService instead of the regular Service. Here is the main receiver:</p>
  
  <pre><code>
  context.startService(new Intent(context, NotificationService.class));
  </code></pre>
  
  <p>And the service</p>
  
  <pre><code>
  public class NotificationService extends IntentService
  {
      public NotificationService()
      {
          super("Notification Service");
      }
  
      @Override
      public void onHandleIntent(Intent intent)
      {
          // this is where I created my notifications
      }
  }
  </code></pre>
  
  <p>The last little trick is to create your AlarmManager again in your application. This will replace the AlarmManager that may have been created in the Boot Receiver. Notice PendingIntent.FLAG_CANCEL_CURRENT. This is done so when the user launches your application, your AlarmManager gets registered if it wasn't already registered on a device boot.</p>
  
  <pre><code>
  AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, NotificationReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT);
  alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, Utils.getNow(), 60 * 1000, pendingIntent);
  </code></pre>
  
  <p>Hope this helps.</p>