Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotificationListenerService: NullPointerException on getActiveNotifications

I'm trying to implement the NotificationListenerService in my application according to this tutorial: http://www.kpbird.com/2013/07/android-notificationlistenerservice.html ,but i'm having a NullPointerException when calling getActiveNotifications.

Caused by: java.lang.NullPointerException
at android.os.Parcel.readException(Parcel.java:1437)
at android.os.Parcel.readException(Parcel.java:1385)


at android.app.INotificationManager$Stub$Proxy.getActiveNotificationsFromListener(INotificationManager.java:500)
at android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:149)
at com.rootsoft.rsnotificationservice.RSNotificationService.activeNot(RSNotificationService.java:85)
at com.rootsoft.rsnotificationservice.RSNotificationService.access$0(RSNotificationService.java:81)
at com.rootsoft.rsnotificationservice.RSNotificationService$1.onReceive(RSNotificationService.java:105)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:763)
... 9 more

I'm sending a broadcast to the service which should generate a list of all Notifications:

private void activeNot () {
    List l = new List();
    l.Initialize();

    for (StatusBarNotification sbn : getActiveNotifications() ) { <---- Error happens here
        l.Add(sbn);
    }

    Log.i("B4A", "List created.");


    }
}
like image 633
Verhelst Avatar asked Sep 08 '13 14:09

Verhelst


People also ask

What is NotificationListenerService?

android.service.notification.NotificationListenerService. A service that receives calls from the system when new notifications are posted or removed, or their ranking changed.

How do I turn on notification listener service?

You need to grant access to your app to read notifications: "Settings > Security > Notification access" and check your app.

How do I use notification listener service in Android?

To use NotificationListenerService, we need to create a java file which extends NotificationListenerService and implement two callback methods. Both methods have a parameter named “sbn”, which is an object of StatusBarNotification class. StatusBarNotification provides necessary information about Notifications.


1 Answers

EDIT: I have since learned more about this and got it working!

NOTE: First, make sure you have enabled your app in the Notification Access settings pane of your Android device.

I had the exact same problem until now. Turns out, overriding onBind is dangerous. If you do override onBind, you have to return the same IBinder that super.onBind(intent) returns. If you want to return your own custom binder, make sure you use a unique intent, and only return your custom binder when the custom intent is received.

@Override
public IBinder onBind(Intent intent)
{
    if (intent.getAction().equals("custom_intent"))
    {
        return customBinder;
    }
    else
    {
        return super.onBind(intent);
    }
}

The system calls onBind on your service, once you have granted it permission to read Notifications. If your onBind returns a custom binder to the system, the system will not give you the notifications, and could lead to Null Pointer or Security Exceptions.

Hope this helped!

like image 57
demonslayer319 Avatar answered Oct 06 '22 03:10

demonslayer319