I have a NotificationService class that listens to notifications. However when I call getActiveNotifications(), it throws a SecurityException. Yes I have checked for permission before calling this method.
I use AsyncTask inside NotificationService to get notification. Code is below.
private class AsyncProcessNotification extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
int notificationType = MainApplication.settingNotificationType;
MainApplication.clearNotificationItems();
if (MainApplication.settingNotificationType == Constants.KEY_NOTIFICATION_TYPE_DISABLED) {
Log.i(TAG, "Notifications disabled");
return null;
}
if (PermissionHelper.isNotificationPermissionGranted(getApplicationContext())) {
if (getActiveNotifications() == null || getActiveNotifications().length == 0) {
Log.i(TAG, "No notifications found");
return null;
}
Log.i(TAG, "Getting " + getActiveNotifications().length +" notifications");
Log.i(TAG, "Notification type " + notificationType);
for (StatusBarNotification statusBarNotification : getActiveNotifications()) {
// Process notifications
}
} else {
//
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Intent notify = new Intent(Constants.ACTION_NOTIFICATION_LISTENER);
sendBroadcast(notify);
}
}
Strange thing is according to
Crashlytics, sometimes it fails at if (getActiveNotifications() == null || getActiveNotifications().length == 0)
and sometimes it fails at Log.i(TAG, "Getting " + getActiveNotifications().length +" notifications");
To check permission, I use following method.
public static boolean isNotificationPermissionGranted(Context context) {
Set<String> appList = NotificationManagerCompat.getEnabledListenerPackages(context);
for (String l:appList) {
if (l.equals(context.getPackageName())) {
return true;
}
}
return false;
}
Stack trace:
Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by java.lang.SecurityException: Disallowed call from unknown notification listener: android.service.notification.INotificationListener$Stub$Proxy@3e9880d
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.app.INotificationManager$Stub$Proxy.getActiveNotificationsFromListener(INotificationManager.java:1046)
at android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:467)
at android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:420)
at com.afd.app.lockscreen.ios11.lib.service.NotificationService$AsyncProcessNotification.doInBackground(NotificationService.java:120)
at com.afd.app.lockscreen.ios11.lib.service.NotificationService$AsyncProcessNotification.doInBackground(NotificationService.java:97)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
I know I must be doing something stupid but can not figure out what. Can somebody please help? Thanks!
A service that receives calls from the system when new notifications are posted or removed, or their ranking changed.
A notification listener service allows the Google App to intercept notifications posted by other applications. Within the Android Manifest file is the inclusion of the new Notification Listener Service.
For those running versions of Android higher than 5.0, please enable our Notification listener under Settings > Sound and notifications > Notification access.
I hit the same error in my notification listener service. I was able to solve it by waiting for the listener service to get connected to the notification manager before calling any of the NotificationListenerService methods.
@Override
public void onListenerConnected() {
Log.i(TAG, "Listener connected");
listenerConnected = true;
}
private class AsyncProcessNotification extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
...
// wait until our notification listener service is connected
// to the notification manager
Log.i(TAG, "Waiting for listener to be connected...");
while(!listenerConnected);
// Call getActiveNotifications after this
for (StatusBarNotification sbn : getActiveNotifications()) {
processNotifications(sbn);
}
}
// rest of the AsyncTask here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With