CitoPaez
CitoPaez

Reputation: 69

Get all Android Lollipop notifications by code

I want to make a Notification applet for Qualcomm Toq Smartwatch. It has a prebuild one but it does not work properly with Android Lollipop, so I decided to create one myself. I read about using Accessibility Service, but it does not work for me. Can someone find whats wrong with my code? I double checked and "Settings > Accessibility" is enabled for the service, so it seems that is not the problem.

Android manifest:

    <service android:name=".ToqAccessibilityService"
        android:label="@string/service_name"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/accessibilityservice" />
    </service>

Then the code:

public class ToqAccessibilityService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent e) {
    Log.d("ToqAN","Recieved event: "+AccessibilityEvent.eventTypeToString(e.getEventType()));
    Parcelable data = e.getParcelableData();
    if (data instanceof Notification) {
        Log.d("ToqAN","Recieved notification");
        Notification notification = (Notification) data;
        Log.d("ToqAN","ticker: " + notification.tickerText);
        Log.d("ToqAN","icon: " + notification.icon);
        Log.d("ToqAN", "notification: "+ e.getText());
    }
}

@Override
public void onInterrupt() {}

}

The config xml

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeNotificationStateChanged"
android:accessibilityFeedbackType="feedbackAllMask"
android:notificationTimeout="100"
android:description="@string/accessibility_service_description"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="false"/>

If I change "typeNotificationStateChanged" for "typeAllMask" I get all type of click and window events, but not a single one about notifications. Does anyone what may be wrong in my code? I know that there are a lot of similar questions, but I did not found any solution in those entries.

Thank you very much in advance!

Upvotes: 2

Views: 746

Answers (1)

CitoPaez
CitoPaez

Reputation: 69

I got a solution for the problem. Instead of using AccessibilityService, I used NotificationListenerService.

Android Manifest:

    <service android:name=".NotificationListener"
        android:label="@string/service_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

NotificationListener class:

public class NotificationListener extends NotificationListenerService {
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        Notification n = sbn.getNotification();
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        Notification n = sbn.getNotification();
    }
}

Finally, you have to enter "Settings > Sound & notification > Notification access" and enable your app to access notifications.

This way I managed to get the notification when created and when removed.

If someone achieves the same using AccessibilityService, please post an answer.

Upvotes: 2

Related Questions