Stack Exchange
OnListenerCOnnected in NotificationListenerService not being called
My app only does not work on Huawei and on other phones it does. Why???
I have that code in my MainActivity:
        package pl.ct8.wieprzco.wieprzwatch;
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
    startActivity(intent);
    startService(new Intent(this,NotificationGetService.class));
}

}
And that simple code in NotificationGetService class:
public class NotificationGetService extends NotificationListenerService{
SharedPreferences sharedPreferences;
SharedPreferences.Editor spEditor;

@Override
public void onCreate(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    spEditor = sharedPreferences.edit();
    Log.e("jej","swiat powiadomien");
    super.onCreate();
}

@Override
public void onListenerConnected(){
    super.onListenerConnected();
    Log.e("startCommand","yeah");
}

@Override
public int onStartCommand(Intent intent,int flags, int startId){
    Log.e("startCommand","yes");
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onNotificationPosted(StatusBarNotification sbn){
    super.onNotificationPosted(sbn);
    spEditor.putInt("notificationCount",sharedPreferences.getInt("notificationCount",0)+1);
    spEditor.apply();
    Log.e("ADDED","YESSSSSS");

}

@Override
public void onNotificationRemoved(StatusBarNotification sbn){
    super.onNotificationRemoved(sbn);
    spEditor.putInt("notificationCount",sharedPreferences.getInt("notificationCount",1)-1);
    spEditor.apply();
    Log.e("REMOVED","YESSSSSS");
}

}
That is my manifest:
<?xml version="1.0" encoding="utf-8"?>


<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <service android:name=".UpdateTimeService"/>
    <service android:name=".NotificationGetService"
        android:label="@string/service_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
    </service>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


</application>


And it logs onCreate and onStartCommand, but no onListenerConnected or other. Please help i try to do that so much time.
Very thanks John!!

With me, the first time the app is installed on the phone it didn't work. I had to go to setting --> notification access --> turn on the notificationListener.
After I did this, it worked.
When I made some changes, it stopped working. Turned out Android has a caching problem, as you can read here. What you should do is rename (refactor) the notificationListenerService and it should work.

You actually saved me with "What you should do is rename (refactor) the notificationListenerService and it should work.". I tried a lot of ways but nothing work untill I changed the listener name. Thanks so much

I tried refactoring - no luck. Only uninstall my app, reboot device, then install app worked for me. that's a nightmare debugging, uhhh.

Maybe it's just because you typed in the wrong tag for loggin?
In your snippet:
@Override
public void onListenerConnected(){
    super.onListenerConnected();
    Log.e("startCommand","yeah");
}

@Override
public int onStartCommand(Intent intent,int flags, int startId){
    Log.e("startCommand","yes");
    return super.onStartCommand(intent, flags, startId);
}

These two Log.e() calls both have tag "startCommand"...
Just in case relevant: the NotificationListernerService.onBind() method shouldn't be overridden... Overridding it will prevent onListenerConnected() from being called...

In my experience, messing with onBind() will also prevent it from being called.

Have you declared the necessary permission and intent filter in your Manifest for the service as following?
<service android:name=".NotificationListener"
      android:label="@string/service_name"
      android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
 <intent-filter>
     <action android:name="android.service.notification.NotificationListenerService" />
 </intent-filter>



Yes i have got it

And same problems

Have you checked this bug in Android?
https://issuetracker.google.com/issues/36984668
The issue report reads:

I'm not seeing this issue in development, but ever since android 4.4
  came out I'm getting many reports from users that my app notification
  monitoring just stops when they update the app. It's as if the
  NotificationListenerService just becomes totally inactive. 
The notification listener is still showing as on in their phones and
  my check to see if the listener is still enabled also show that it is
  enabled. 
I'm having to then get users to reboot before they start getting any
  notifications again. I didn't see this behaviour in android 4.3 
I can't even think of any way to programatically do anything about
  that as I guess there's no way to stop and restart the notification.


You might be debugging your application.
There are some issues while using NotificationListenerService in debugging mode.
You should run your app, give permission to the app to listen to notifications.
IF YOU HAVE ALREADY GIVEN THE PERMISSION, THEN REVOKE IT AND GRANT IT AGAIN.
(If required, reboot your phone)
The debugger will then hit.

This also happens if you run on a device running Android 4.3 or 4.4 because onListenerConnected() was not introduced until Android 5.0.
Instead, listen to onBind() on these older devices.
