title: Why is this NotificationListenerService not working
body: <p>I'm fighting against the NotificationListenerService without much luck.
  I tried a lot of recipes found here and there, especially on so... But it works quite inconsistently.</p>
  
  <p>A first look at the code : </p>
  
  <p>Manifest : </p>
  
  <pre><code>&lt;service
      android:name=".services.NLService"
      android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" &gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.service.notification.NotificationListenerService" /&gt;
      &lt;/intent-filter&gt;
  &lt;/service&gt;
  </code></pre>
  
  <p>Then the service itself :</p>
  
  <pre><code>public class NLService extends NotificationListenerService {
  
      private String TAG = "NLService";
  
      // bind and unbind seems to make it work with Android 6...
      // but is never called with Android 4.4... 
      @Override
      public IBinder onBind(Intent mIntent) {
          IBinder mIBinder = super.onBind(mIntent);
          Log.i(TAG, "onBind");
          return mIBinder;
      }
  
      @Override
      public boolean onUnbind(Intent mIntent) {
          boolean mOnUnbind = super.onUnbind(mIntent);
          Log.i(TAG, "onUnbind");
          isNotificationAccessEnabled = false;
          try {
          } catch (Exception e) {
              Log.e(TAG, "Error during unbind", e);
          }
          return mOnUnbind;
      }
  
      // onCreate is called with Android 4.4 
      // because the service is explicitly started from the MainActivity.
      // Not on Android 6 where the system binds this service itself...
  
      @Override
      public void onCreate() {
          super.onCreate();
          Log.i(TAG, "**********  onCreate");
  
      @Override
      public void onNotificationPosted(StatusBarNotification sbn) {
  
          Log.i(TAG, "**********  onNotificationPosted");
          Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());
      }
  
      @Override
      public void onNotificationRemoved(StatusBarNotification sbn) {
          Log.i(TAG, "********** onNOtificationRemoved");
          Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());
      }
  }
  </code></pre>
  
  <p>And in the main Activity the service is started or we ask the user to enable the setting if needed :</p>
  
  <pre><code>if (!Utilities.hasNotificationAccess(this)) 
  {
       Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
       startActivity(intent);
       Log.i(TAG,"hasNotificationAccess NO");
  }
  else
  {
      Log.i(TAG,"hasNotificationAccess YES");
  
      // without this startService, it never works with Android 4.4...
      // but this is not needed in Android 6... 
      Intent mServiceIntent = new Intent(this, NLService.class);
      startService(mServiceIntent);
  }
  </code></pre>
  
  <p>Obviously, the secure setting access for notifications is enabled...</p>
  
  <p>On Android 6 :</p>
  
  <p>On the NLService itself, the addition of the onBind and onUnbind methods make it work, I can se the onBind log, and the onNotificationPosted is triggered nicely. But it lasts until the next launch of the app, then no more binding, no more onNotificationPosted, just <em>nothing</em> happens until I go back to security settings to uncheck-recheck the notifications access: repeating this each time the App is started is not something possible in a production environment.</p>
  
  <p>On Android 4.4 : </p>
  
  <p>In contrast to Android 6, in the MainActivity I need to explicitly start the NLService otherwise it never stats by itself. But once again it works for the first launch and never works again until uncheck-recheck the security setting... </p>
  
  <p>Did I miss something obvious ? </p>