Notification not triggered when app is closed
<p>I am trying to set an Alarm manager so that it runs every 15 minutes, checks if anything has been added to the database and then shows a notification. However, whilst I'm just testing this, I am trying to show the notification after 60 seconds. </p>

<p>The below code works perfectly when the app is open or minimised but is never called when the app is closed. </p>

<p><strong>Main Activity (onCreate())</strong></p>

<pre><code>Intent alarm = new Intent(this.context, AlarmReceiver.class);
    boolean alarmRunning = (PendingIntent.getBroadcast(this.context, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
    if (!alarmRunning) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 0, alarm, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 60000, pendingIntent);
    }
</code></pre>

<p><strong>Alarm Receiver</strong></p>

<pre><code>public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent background = new Intent(context, BackgroundService.class);
    context.startService(background);
}
</code></pre>

<p>}</p>

<p><strong>Background Service</strong></p>

<pre><code>public class BackgroundService extends Service {

private final String TAG = getClass().getName();

private Boolean isRunning;
private Context context;
private Thread backgroundThread;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    this.context = this;
    this.isRunning = false;
    this.backgroundThread = new Thread(myTask);
}

private Runnable myTask = new Runnable() {
    @Override
    public void run() {
        Log.d(TAG, "THE BACKGROUND SERVICE IS RUNNING");
        //Run actions
        showNotification(context);
        //
        stopSelf();
    }
};

private void showNotification(Context context) {
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("Notification!") // title for notification
            .setContentText("Alarm Received")
            .setSound(soundUri)// message for notification
            .setAutoCancel(true); // clear notification after click
    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());
}

@Override
public void onDestroy() {
    this.isRunning = false;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (!this.isRunning) {
        this.isRunning = true;
        this.backgroundThread.start();
    }
    return START_STICKY;
}
</code></pre>

<p>}</p>

<p><strong>Manifest</strong></p>

<pre><code>&lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;

    &lt;service android:name=".notification.BackgroundService" /&gt;

    &lt;receiver android:name=".notification.AlarmReceiver"
        android:process=":remote"
        android:enabled="true"
        android:exported="true"&gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;
</code></pre>

<p>I have read several posts but cannot seem to find the answer on how to resolve this so that the notification can still be shown/checked for when the app is closed and by closed I mean where the user removes the app from the running apps list. </p>