Detect a new Android notification

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

Leave a Comment