2

I have an app that uses the NotificationListenerService. It works perfectly on apis lower than Android Oreo, but specifically at Android Oreo, the system seems to not start the service when the user restart the app (it works at the moment the user grants the permission for the first time), even though the permission is already granted. I could not find any solution on StackOverflow for this problem in specific.


AndroidManifest

<service android:name=".Services.MyCustomNotificationListener"
android:label="MyAppName"
android:exported="true"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
    <action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>


Augusto Carmo
  • 4,386
  • 2
  • 28
  • 61
  • oreo has new change regarding notifications channel, please look into it https://developer.android.com/reference/android/service/notification/NotificationListenerService.html – vikas kumar Dec 13 '17 at 15:19
  • Thank you for the reply, @vikas. Could you elaborate more on your answer, please? – Augusto Carmo Dec 13 '17 at 15:31
  • there was an announcement in Google IO this year that if you are targetting the Oreo then you should add your notifications through channels only otherwise they will be dropped if this is the case with you can look more into it. – vikas kumar Dec 13 '17 at 15:34
  • Actually, I am interested in listening to other apps notifications. – Augusto Carmo Dec 13 '17 at 15:35
  • @AugustoCarmo - have you resolved this as I have the same issue. – John Smith Jan 04 '18 at 20:45
  • @John Smith, I have not found a solution for this yet =/ – Augusto Carmo Jan 05 '18 at 09:20
  • @AugustoCarmo - I have now been running this for one day and seems to be working great. I have yet to verify whether or not the onListenerDisconnect is necessary but it is working as is. If you can verify it as an legit answer, I will modify my post to remove the disclaimers. – John Smith Jan 05 '18 at 22:50
  • Sorry for the late reply, @John Smith, and thank you very much for your post. But, unfortunately, a partner of mine has already tried this before without any success =/. – Augusto Carmo Jan 08 '18 at 10:18
  • That is strange because I'm using it and it's working perfectly and have actually released it to Play. I recommend just using the code I included and watching logcat. Make sure you added the correct info in the manifest as well. This definitely works. – John Smith Jan 14 '18 at 05:32
  • are you using api 27? – Jyoti JK Apr 12 '18 at 11:21

2 Answers2

1

Ok, THIS ISN'T WELL TESTED. I just wanted to throw it up there in hopes of helping others with the same issue. It seems that you have to use onListenerConnected and getActiveNotification() to make OREO devices receive notifications. Here is my code:

public class ListenForNotificationsService extends NotificationListenerService {

    private String TAG = this.getClass().getSimpleName();

    @Override
    @TargetApi(Build.VERSION_CODES.N)
    public void onListenerConnected() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            getActiveNotifications();
        }
        Log.d(TAG, "Listener connected");
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {

        Log.d(TAG, "Notification received");
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {

        if (sbn.getPackageName() != null) {

            Log.d(TAG, "Notification removed:" + sbn.getPackageName() + ":" + sbn.getId());
        }
    }

    @Override
    @TargetApi(Build.VERSION_CODES.N)
    public void onListenerDisconnected() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // Notification listener disconnected - requesting rebind
            requestRebind(new ComponentName(this, NotificationListenerService.class));
        }
    }
}

Please note, I don't know if the onListenerDisconnected() is correct or even necessary so use with caution. Another thought as well is that because of the DEEP SLEEP changes in OREO there may be other things needed to handle that. YMMV, It was just that this wasted a lot of my time so hopefully it will help someone else.

John Smith
  • 3,493
  • 3
  • 25
  • 52
  • I used [this](https://gist.github.com/TeacherWang/5cf7297865710d0925cc58f03609c2e0) before Oreo. Doesn't work anymore. Your answer unfortunately doesn't work either. – NoHarmDan Apr 10 '18 at 16:33
  • @NoHarmDan, I don't understand, the exact code I posted, I have been using since I made the post in an app and it is working fine. Did you try rebooting your phone? The Notification permission is a bit finicky. Also check the logs – John Smith Apr 10 '18 at 20:31
  • Of course I did, I tried all kinds of stuff, I'm no rookie in dealing with Android's bullshit. :D Seems like the problem is only persistent while debugging - the release version seems to be working fine, tho I have some reports of users having issues. But that's the problem, cannot fix those issues as long as debug and release act differently. – NoHarmDan Apr 10 '18 at 21:08
  • @JohnSmith I tried your solution but works only once.What is the exact solution NotificationListenerService starting on Android oreo or higher? – TsigumEnes Oct 26 '18 at 08:31
0

Had the same problem. Got it working by changing battery saver options for my app. Also allowed it to autostart.