Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to cancel all incoming notifications in android programmatically

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 5.20k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: