Intercept all Android notifications
/**
* First, extend NotificationListener and override necessary methods
*/
public class NotificationListener extends NotificationListenerService{
// override methods here
}
/**
* Next, add all required permissions and declarations to your manifest
*/
<uses-permission
android:name="android.permission.WRITE_SECURE_SETTINGS"
tools:ignore="ProtectedPermissions"
/>
<service
android:name=".NotificationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:enabled="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
/**
* In your activity, check if service is enabled in system, if not enable it
* NOTE: Service can be enabled programmatically but then must be installed
* as a system app
*/
// Button 1
readServicesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentResolver contentResolver = context.getContentResolver();
String enabledListeners = Settings.Secure.getString(
contentResolver, "enabled_notification_listeners");
Log.d("SERVICES", enabledNotificationListeners);
}
});
// Button 2
writeServiceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentResolver contentResolver = context.getContentResolver();
Settings.Secure.putString(
contentResolver, "enabled_notification_listeners",
enabledListeners + ":"
+ "your.package.name/your.package.name.NotificationListener");
Log.d("SERVICES", "Added!");
}
});
// NOTE: Don't forget the ":" between previous enabled services and appending yours