SoulRayder
SoulRayder

Reputation: 5166

Tracking push notifications in Android

Is it possible to track push notifications programmatically in android?

I mean to say is it possible to track the notifications appearing in the notification bar through some event, or service?

I have tried using AccessibilityService, but I haven't been able to track it using the TYPE_NOTIFICATION_STATUS_CHANGED event.

Upvotes: 1

Views: 2148

Answers (1)

csenga
csenga

Reputation: 4114

This can be done using NotificationListenerService from API 18.

A working example:

The service:

public class Test extends NotificationListenerService {


    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);

        //do your job
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);

        //do your job
    }
}

Manifest:

<service android:name=".Test"
            android:label="Test"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>

To get this work, you must enable the service which can be done through a settings link:

  Intent i=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
                startActivity(i);

Upvotes: 4

Related Questions