Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect a new Android notification

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?

like image 388
Sean Avatar asked Feb 20 '12 19:02

Sean


People also ask

Is there a notification log on Android?

Open the Settings app, or pull down on the notification shade and tap on the Settings icon (gear-shaped). Tap on Notifications. Tap on Notification history.

What is a notification listener?

↳ 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 get rid of Android setup notifications?

You can manually disable the setup wizard on the device. From Android Setup > Apps & Notifications. Search for All apps and show System apps (hidden by default). Select and disable Android Setup.


1 Answers

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

like image 79
Force Avatar answered Oct 14 '22 15:10

Force