Search code examples
androidandroid-studioaccessibilityservice

How to read Notifications using Accessibility Services


I am trying to get incoming messages and print them. With the help of some articles, I have managed to print but with this, I am only managed to get the notification title. I am new to android development and don't know if is it possible to get an actual message body with accessibityservice.

Manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.domain.accessibilitydemo">
    <uses-permission  android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name=".AccessibilityActivity"
            android:enabled="true"
            android:exported="true"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>

            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/serviceconfig" />
        </service>
    </application>

</manifest>

Serviceconfig.xml :

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:accessibilityFlags="flagDefault"
    android:canRequestEnhancedWebAccessibility="true"
    android:notificationTimeout="100"
    android:packageNames="@null"
    android:canRetrieveWindowContent="true"
    android:canRequestTouchExplorationMode="true"  />

AccessibiliActivity :

package com.domain.accessibilitydemo;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.view.accessibility.AccessibilityEvent;

public class AccessibilityActivity extends AccessibilityService {
    String txtMessage;

    @Override
    public void onInterrupt() {

    }
    @Override
    protected void onServiceConnected() {
        System.out.println("onServiceConnected");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
        info.notificationTimeout = 100;
        info.packageNames = null;
        setServiceInfo(info);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
        //if(event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED){
            if (event.getPackageName().toString().equals("com.whatsapp")){
                StringBuilder message = new StringBuilder();
                if (!event.getText().isEmpty()) {
                    for (CharSequence subText : event.getText()) {
                        message.append(subText);
                    }
                    txtMessage = message.toString();
                    MainActivity.textMsg.setText(txtMessage);



                }
            }
        }
    }

}

OUTPUT :

OUTPUT SCREENSHOT

How can I access the actual message? and is there any way to store them inside a listview?

Thanks


Solution

  • https://developer.android.com/reference/android/view/accessibility/AccessibilityRecord#getParcelableData()

    Different types of event carry different data; For this particular kind you use getParcelableData() to get the notification body.

    case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
                    Notification notification = (Notification) accessibilityEvent.getParcelableData();
                    if (notification == null) return;
                    String title = notification.extras.getCharSequence(Notification.EXTRA_TITLE).toString();
                    String text = notification.extras.getCharSequence(Notification.EXTRA_TEXT).toString();
                    String text2 = notification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT).toString();
                    String package_name = accessibilityEvent.getPackageName().toString();
                    break;