30

In the Android app that I'm working on, I'd like to be able to detect when a new status bar notification appears, regardless of if it was caused by my app. To be more specific, I want to count the number of notifications in a given time frame.

Is this even possible, and if so, how?

Sean
  • 5,244
  • 6
  • 28
  • 27

2 Answers2

57

Actually, it is possible, I use it in my app.

For Android 4.2 and below:

You need to register an AccessibilityService and make sure the user enables the service.

Example for a service:

public class InstantMessenger extends AccessibilityService {

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
        //Do something, eg getting packagename
        final String packagename = String.valueOf(event.getPackageName());  
}
}

@Override
protected void onServiceConnected() {
    if (isInit) {
        return;
    }
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
    setServiceInfo(info);
    isInit = true;
}

@Override
public void onInterrupt() {
    isInit = false;
}
}

Example for checking if your Service is activated

For Android 4.3 and above:

Use the Notification Listener API

Force
  • 6,312
  • 7
  • 54
  • 85
  • 12
    @Sean Note: To start a `AccessibilityService` the user needs to go open Settings--Accessibility, enable Accessibility, then separately enable your service. At which point, they are presented with the following message: "This accessibility service may be able to collect all the text you type, including personal data credit cards except passwords. It may also log your user interface interactions. It comes from the application xxx. Use...?" Unless you are using this for personal use, or for a business, this might be a lot of trust to expect from Market users. Just want to give you the big picture – koopaking3 Mar 09 '12 at 17:13
  • 1
    Doesn't mean you shouldn't use it, just be aware before you start putting the work in to build your own – koopaking3 Mar 09 '12 at 17:14
  • Thats true, but this screen does also show up when you are activating a new keyboard. Still better than requesting root and as far as I am concerned the only available option (especially as this is exactly what OP requested) – Force Mar 09 '12 at 20:26
  • 1
    Agreed; if a developer is willing to take this route then it is really the only way that solves the problem (and very nicely). Your answer deserves an upvote for that! – koopaking3 Mar 09 '12 at 21:00
  • Can anyone help/explain me what does AccessibilityServiceInfo.FEEDBACK_SPOKEN; suggest for Notification state change? – Himanshu Virmani Aug 27 '13 at 11:15
  • 4
    Android 4.3 and above should prefer the [Notification Listener API](http://developer.android.com/about/versions/android-4.3.html#NotificationListener) over this, as suggested by Google. Please see answer below by @thoutbeckers – Mohnish Mar 25 '14 at 05:34
  • using this method, `onAccessibilityEvent` is also called when a new `Toast` appears, which is not what I want. I want only status bar notification – Hải Phong Oct 09 '14 at 16:01
  • Is it possible to know when the toast gets hidden, and not just shown? Is it possible to also perform action on it, to hide it? – android developer May 09 '19 at 17:54
22

The new Notification Listener API in Android 4.3 enables you to do this.

With this there is less need for the accessibility hack. It also allows you to dismiss notifications.

thoutbeckers
  • 2,599
  • 22
  • 24
  • Notification listener does not work completely yet, for example you can't retrieve the text from a Google navigation update (always returns null). Which means the accessibility 'hack' is still useful – Fuzzy Apr 20 '14 at 08:40
  • changed it to "less need" instead of "no need" – thoutbeckers Apr 22 '14 at 10:53
  • 2
    is it possible to *change* the notifications in any way? i want to make a certain app's notifications unwithdrawable by that application – squirrel Jun 18 '15 at 19:18
  • But I can't get application name for notification, right? – Anton Putau Apr 28 '17 at 11:09