title: Android permisson BIND_NOTIFICATION_LISTENER_SERVICE is not working
body: <p>I'm working in React Native Application. I'm trying hard in Android to make use of <code>Notification Services</code> &amp; <code>BIND_NOTIFICATION_LISTENER_SERVICE</code>. I'm trying to read the incoming notifications using <code>NotificationListenerService</code>.</p>
  
  <p>I'm using <strong>One Plus 5T (Android Oreo)</strong> for debugging.</p>
  
  <p>I've seen lots of duplicate questions and <strong>I've tried</strong> the following solutions which I got from those questions.</p>
  
  <blockquote>
    <p>1.Restarting the phone (Sounds silly and it doesn't work) </p>
    
    <p>2.Re-install/Re-build the application  </p>
    
    <ol start="3">
    <li>Renaming the NotificationListenerService class file and run Etc..</li>
    </ol>
  </blockquote>
  
  <p><strong>Problems I've  been facing,</strong> </p>
  
  <blockquote>
    <p>1.Cannot see my application in 'Notification Access' settings.</p>
    
    <p>2.onCreate / onNotificationPosted is not getting called when I receive any Notification</p>
    
    <p>3.Checked whether my application is having Notification permisson or not using the below code and it returned 'false' (Obviously, that's my
    problem)</p>
  </blockquote>
  
  <pre><code>ComponentName cn = new ComponentName(getReactApplicationContext(), NotificationListener.class);
  String flat = Settings.Secure.getString(getReactApplicationContext()
                     .getContentResolver(), "enabled_notification_listeners");
  final boolean hasPermission = flat != null &amp;&amp; flat.contains(cn.flattenToString());
  // hasPermission - false
  </code></pre>
  
  <p>Since <code>BIND_NOTIFICATION_LISTENER_SERVICE</code> is not listed <code>Dangerous permissions</code> list, I thought defining it in Manifest (Source is given below for your reference) is enough instead of asking run time permission. </p>
  
  <p>I've followed <a href="http://www.androiddevelopersolutions.com/2015/05/android-read-status-bar-notification.html" rel="nofollow noreferrer">this</a> tutorial and referred other tutorials also. I can't find out where I'm doing wrong. Everything looks perfect. After scratching my mind for the whole day, I thought I'd ask your help. </p>
  
  <p>Find my source code below. </p>
  
  <h2>Your time and patience are appreciated. (:</h2>
  
  <p><strong>My Source</strong></p>
  
  <p><em>AndroidManifest.xml</em></p>
  
  <pre><code>      &lt;service
              android:name=".NotificationListener"
              android:label="@string/app_name"
              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><em>NotificationListener.java</em> (Not even single log printed)</p>
  
  <pre><code>public class NotificationListener extends NotificationListenerService {
  
      Context context;
  
      private String TAG = this.getClass().getSimpleName();
  
      @Override
      public void onCreate() {
          super.onCreate();
          Log.d("KBT", "FROM LISTENER onCreate");
          context = getApplicationContext();
      }
  
      @Override
      public void onNotificationPosted(StatusBarNotification sbn) {
  
          Log.d("KBT", "FROM LISTENER onNotificationPosted");
  
          String pack = sbn.getPackageName();
          String ticker ="";
          if(sbn.getNotification().tickerText !=null) {
              ticker = sbn.getNotification().tickerText.toString();
          }
          Bundle extras = sbn.getNotification().extras;
          String title = extras.getString("android.title");
          String text = extras.getCharSequence("android.text").toString();
  
          Intent msgrcv = new Intent("NOTIFICATION_POSTED_KBT");
          msgrcv.putExtras(extras);
  
          LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
      }
  
      @Override
      public void onNotificationRemoved(StatusBarNotification sbn) {
          Log.i(TAG, "********** onNOtificationRemoved");
      }
  
  }
  </code></pre>
  
  <p><em>NotificationListenerModule.java</em></p>
  
  <pre><code>public class NotificationListenerModule extends ReactContextBaseJavaModule {
      Context mainContext;
      public NotificationListenerModule(ReactApplicationContext reactContext) {
          super(reactContext);
          mainContext = getReactApplicationContext();
      }
  
      @Override
      public String getName() {
          return "BatteryStatus";
      }
  
      @ReactMethod
      public void registerNotificationEvent(Callback successCallback) {
  
          // This log is printed successfully hence this function is getting called.
          Log.d("KBT","registerNotificationEvent called");
  
          // Registering Receiver
          LocalBroadcastManager.getInstance(mainContext).registerReceiver(myReceiver, new IntentFilter("NOTIFICATION_POSTED_KBT"));
  
          // Check Notification Permission in our app
          ComponentName cn = new ComponentName(getReactApplicationContext(), NotificationListener.class);
          String flat = Settings.Secure.getString(getReactApplicationContext().getContentResolver(), "enabled_notification_listeners");
          final boolean enabled = flat != null &amp;&amp; flat.contains(cn.flattenToString());
  ]    }
  
  
      private class NLServiceReceiver extends BroadcastReceiver {
  
          @Override public void onReceive(Context context, Intent intent) {
              Bundle bundle = intent.getExtras();
              JSONObject json = new JSONObject();
              Set&lt;String&gt; keys = bundle.keySet();
              for (String key : keys) {
                  try {
                      // json.put(key, bundle.get(key)); see edit below
                      json.put(key, JSONObject.wrap(bundle.get(key)));
                  } catch(JSONException e) {
                      //Handle exception here
                  }
              }
  
              // Emitting the event to React Native
              getReactApplicationContext()
                          .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                          .emit("onNotificationPosted", json);
          }
      }
  }
  </code></pre>
  
  <p><em>Logs</em> (com.logcharge is my package name)</p>
  
  <pre><code>    09-23 10:19:23.968 1035-1653/? E/InputDispatcher: channel '2888e2a com.logcharge/com.logcharge.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
  09-23 10:19:24.175 2465-3354/? E/RecentsTaskLoader: Unexpected null component name or activity info: ComponentInfo{com.logcharge/com.logcharge.MainActivity}, null
  09-23 10:19:24.177 2465-3354/? E/RecentsTaskLoader: Unexpected null component name or activity info: ComponentInfo{com.logcharge/com.logcharge.MainActivity}, null
  09-23 10:19:24.662 2668-2668/? E/OPUtils: removeMultiApp ,com.logcharge
  09-23 10:19:26.981 1035-1324/? E/ActivityManager: Failure starting process com.logcharge
      java.lang.SecurityException: Package com.logcharge was not found!
          at com.android.server.pm.PackageManagerService.checkPackageStartable(PackageManagerService.java:4350)
          at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:4446)
          at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:4407)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.server.am.EmbryoHelper.startProcessLocked(EmbryoHelper.java:93)
          at com.android.server.am.Uterus$BirthRunnable.run(Uterus.java:720)
          at android.os.Handler.handleCallback(Handler.java:790)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:164)
          at android.os.HandlerThread.run(HandlerThread.java:65)
  09-23 10:20:19.029 1035-1370/? E/PackageManager.DexOptimizer: Well this is awkward; package com.logcharge.MainApplication had UID -1
      java.lang.Throwable
          at com.android.server.pm.PackageDexOptimizer.performDexOptLI(PackageDexOptimizer.java:150)
          at com.android.server.pm.PackageDexOptimizer.performDexOpt(PackageDexOptimizer.java:126)
          at com.android.server.pm.PackageManagerService.installPackageLI(PackageManagerService.java:19987)
          at com.android.server.pm.PackageManagerService.installPackageTracedLI(PackageManagerService.java:19547)
          at com.android.server.pm.PackageManagerService.-wrap35(Unknown Source:0)
          at com.android.server.pm.PackageManagerService$9.run(PackageManagerService.java:16810)
          at android.os.Handler.handleCallback(Handler.java:790)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:164)
          at android.os.HandlerThread.run(HandlerThread.java:65)
          at com.android.server.ServiceThread.run(ServiceThread.java:46)
  09-23 10:20:21.277 25936-25936/? E/SensorManager: sensorName:BMI160 Accelerometer,isWakeUpSensor:false,callingApp: com.logcharge,callingPid:25936,callingUid:10632
  09-23 10:20:22.014 646-659/? E/cutils: Nothing there yet; let's create it: /storage/emulated/0/Android/data/com.logcharge
  09-23 10:20:22.015 646-659/? E/cutils: Nothing there yet; let's create it: /storage/emulated/0/Android/data/com.logcharge/cache
  </code></pre>