title: NotificationCompat.Builder and startForeground
body: <p>This works:  IntentService with NotificationCompat.Builder providing a notification to use with NotificationManager.notify()  .setContentIntent(pendingIntent) is needed.  When notify is sent, the notification appears in the notification AND IS PERSISTENT (stays alive until the user clicks it, at which point it starts the activity specified in .setContentIntent).  Good!
    What does not work: I would like the service to be long-lived like a phone-service, so startForeground() would seem advised.  However, when I include that, the associated notification does indeed appear in the tray as it should, but it is NOT PERSISTENT and disappears when the IntentService ends (unlike above). (The associated notification also uses .setContentIntent and starts another Activity.)
  Any thoughts?  It is critical that the service not die until it detects a certain (rare) "event".  It is also critical that the notification remains alive until the user responds by clicking it!<br>
  Here is the boiled-down code (latter case):  Thanks!</p>
  
  <pre><code>    public class SService extends IntentService {
  public SService() {
      super("SService");                          
  }
  @Override
  protected void onHandleIntent(Intent sIntent) {
  
    //Notification construction:
      Notification notif;
      Context context = this;
  
      NotificationManager mNotificationManager = 
              (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  
      Intent bIntent = new Intent(context, BarNotifActivity.class);       
      PendingIntent pbIntent = PendingIntent.getActivity(context, 0, bIntent,0);
      Notification barNotif;
  
      NotificationCompat.Builder bBuilder =
              new NotificationCompat.Builder(this)
                  .setSmallIcon(R.drawable.ic_launcher)
                  .setContentTitle(getString(R.string.bar_title))
                  .setContentText(getString(R.string.bar_text))
                  //.setAutoCancel(true)
                  .setOngoing(true)
                  .setContentIntent(pbIntent);
      barNotif = bBuilder.build();
      this.startForeground(1, barNotif);
  
      long[] vibration = {0, 300, 1000, 300, 1000, 300, 1000, 300, 1000};
      Intent mIntent = new Intent(context, NotifReceiverActivity.class);
      PendingIntent pIntent = PendingIntent.getActivity(context, 0, mIntent,0);
  
      NotificationCompat.Builder mBuilder =
          new NotificationCompat.Builder(context)
              .setSmallIcon(R.drawable.ic_launcher)
              .setContentTitle(getString(R.string.alert_text))
              .setContentText(getString(R.string.alert_text))
              .setTicker("**Notification Arrived!**")
              .setVibrate(vibration)
              //.setAutoCancel(true)
              .setOngoing(true)
              .setContentIntent(pIntent);
      notif = mBuilder.build();
      notif.flags |= Notification.FLAG_AUTO_CANCEL;
      notif.flags |= Notification.FLAG_INSISTENT;  
  
      try {
          Thread.sleep(7000);                         //Pause 7 sec.
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
      mNotificationManager.notify(1, notif);
  }
  </code></pre>
  
  <p>}</p>