In this tutorial we are going to disable the Notifications and still receive the messages. When new notification is posted or removed in status bar, NotificationListener Service is called by the Android OS. NotificationListenerService has methods onCreate(), onNotificationPosted() and onNotificationRemoved(). cancelAllNotifications() used to inform the notification manager about dismissal of all active notifications. Status bar Notifications may be read by below used Notification Listener class.
NotificationListener.class
Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// Inform the notification manager about dismissal of all notifications.
NotificationListener.this.cancelAllNotifications();
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("Msg", "Notification Removed");
}
AndroidManifest.xml
To extend the NotificationListenerService in our class we need to add permission BIND_NOTIFICATION_LISTENER_SERVICE in our manifest file and include an intent filter with the SERVICE_INTERFACE action.
<service android:name=".NotificationListener"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
2 Comment(s)