title: Foreground-service, Alarm-manager, notificationsnow showing
body: <p>I have some trouble with foreground service, alarmManager and notifications.</p>
  <p>Basicly I'm trying to do service which gives notification every morning. I'm trying to do this with foregroundService and cpuWakeLock but some reason I'm still having trouble to get this notification launch every morning. In emulator everything works fine.</p>
  <p>Main reason to use foreground-service is that without it i havent get working that alarms keep firing when app is closed.</p>
  <p>I'm little bit <strong>desperate</strong>.</p>
  <p>Here is my Code (cleaned version)</p>
  <pre><code>public class MentorService_cleaned extends Service {
  NotificationManager notificationManager;
  
  private PowerManager.WakeLock cpuWakeLock = null;
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
  
      super.onCreate();
  
      Intent notificationIntent = new Intent(this, MainActivity.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
              notificationIntent, 0);
      Notification notification = new NotificationCompat.Builder(this)
              .setSmallIcon(R.mipmap.pficon)
              .setContentTitle(&quot;xxx&quot;)
              .setContentText(&quot;xxx&quot;)
              .setContentIntent(pendingIntent).build();
  
      notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
  
      startForeground(1337, notification);
  
  
      Calendar alarmStartTime = Calendar.getInstance();
      Calendar now = Calendar.getInstance();
  
      int alarmHour = 7;
      int alarmMinute = 0;
  
      alarmStartTime = Calendar.getInstance();
      alarmStartTime.set(Calendar.HOUR_OF_DAY, alarmHour);
      alarmStartTime.set(Calendar.MINUTE, alarmMinute);
      alarmStartTime.set(Calendar.SECOND, 0);
      if (alarmStartTime.before(now)) { //if it's after 7:00 move to next day
          alarmStartTime.add(Calendar.DATE, 1);
      }
  
      Intent _Intent = new Intent(getApplicationContext(), AlertReceiver.class);
      final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, _Intent, PendingIntent.FLAG_UPDATE_CURRENT);
      AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
      alarm.set(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), pIntent);
  
  
      // test pushNotification
      pushNotification();
  
      return START_STICKY;
  }
  
  @Override
  public void onCreate() {
  
      try {
          if ((cpuWakeLock == null) || cpuWakeLock.isHeld() == false) {
              PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
              PowerManager.WakeLock cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, &quot;ShakeEventService onCreate Tag&quot;);
              cpuWakeLock.acquire();
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      if (this.cpuWakeLock.isHeld())
          this.cpuWakeLock.release();
  }
  
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  
  public void pushNotification(){
  
      try {
  
          NotificationCompat.Builder notificBuilder = new NotificationCompat.Builder(this);
          notificBuilder.setContentTitle(&quot;xxx&quot;);
          //notificBuilder.setContentText(nextCard.caption);
          //notificBuilder.setTicker(nextCard.caption);
  
          notificBuilder.setContentText(&quot;xxx&quot;);
          notificBuilder.setTicker(&quot;xxx&quot;);
  
          notificBuilder.setSmallIcon(R.drawable.book);
          notificBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
  
          Intent intent = new Intent(this, MainActivity.class);
          TaskStackBuilder tStackBuilder = TaskStackBuilder.create(this);
          tStackBuilder.addParentStack(Card.class);
          tStackBuilder.addNextIntent(intent);
  
          PendingIntent pendingIntent = tStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
          notificBuilder.setContentIntent(pendingIntent);
          notificationManager = (NotificationManager) getSystemService((Context.NOTIFICATION_SERVICE));
  
          if (PFContents.profile.Notification) {
              notificationManager.notify(1, notificBuilder.build());
          }
      }catch(Exception e)
      {
  
      }
  
  }}
  </code></pre>
  <p>AlertReceicer-class</p>
  <pre><code>public class AlertReceiver extends BroadcastReceiver {
  
  
  @Override
  public void onReceive(Context ctxt, Intent i) {
  
      Intent in = new Intent(ctxt, MentorService.class);
      in.putExtra(&quot;fromMain&quot;, &quot;false&quot;);
      ctxt.startService(in);
  }
  </code></pre>
  <p>}</p>
  <p>And here my manifest file:</p>
  <pre><code>&lt;uses-permission android:name=&quot;android.permission.WAKE_LOCK&quot; /&gt;
  &lt;service
              android:name=&quot;.MentorService&quot;
              android:enabled=&quot;true&quot;
              android:exported=&quot;true&quot;
              android:process=&quot;.myprocess&quot;
              android:stopWithTask=&quot;false&quot; /&gt;
  </code></pre>