在 Android 中跟踪推送通知

Tracking push notifications in Android

是否可以在 android 中以编程方式跟踪推送通知?

我的意思是说是否可以通过某些事件或服务来跟踪出现在通知栏中的通知?

我尝试过使用 AccessibilityService,但无法使用 TYPE_NOTIFICATION_STATUS_CHANGED 事件跟踪它。

这可以使用 API 18 中的 NotificationListenerService 来完成。

一个工作示例:

服务:

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
    }
}

清单:

<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>

要完成这项工作,您必须启用可以通过设置完成的服务 link:

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