Question Detail

How to clear all Notifications from Notification bar programmatically android?

5 years ago Views 2702 Visit Post Reply

I want to remove all the coming notification from the Notification bar. After KitKat version there are too many changes in the Android API, How can I Clear all the Notifications which is placed in the Notification bar.

Please complete process how to implement Clear Notification form the Notification bar Android Programmatically?


Thread Reply

alex levine

- 5 years ago

Create a NotificationListenerService to receive a Notification 

 

Create a java file 

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

public class NotificationListnerService extends NotificationListenerService {
    private String TAG = this.getClass().getSimpleName();
    private NListenerServiceReceiver nlservicereciver;
    String packageName;

    @Override
    public void onCreate() {
        super.onCreate();
        packageName = NotificationListnerService.this.getPackageName();
        nlservicereciver = new NListenerServiceReceiver();
        IntentFilter filter = new IntentFilter();
        pr("**************1***"+packageName);
        filter.addAction(packageName);
        registerReceiver(nlservicereciver, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(nlservicereciver);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        Log.i(TAG, "**********  onNotificationPosted");
        Log.i(TAG, "ID :" + sbn.getId() + " t " + sbn.getNotification().tickerText + " t " + sbn.getPackageName());
        pr("**************2***"+packageName);
        Intent i = new Intent(packageName);
        i.putExtra("notification_event", sbn.getPackageName());
        sendBroadcast(i);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i(TAG, "********** onNOtificationRemoved");
        Log.i(TAG, "ID :" + sbn.getId() + "t" + sbn.getNotification().tickerText + "t" + sbn.getPackageName());

        pr("**************3***"+packageName);
        Intent i = new Intent(packageName);
        i.putExtra("notification_event", sbn.getPackageName().toString());
        sendBroadcast(i);
    }

    class NListenerServiceReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getStringExtra("command")!=null) {
                if (intent.getStringExtra("command")!=null && intent.getStringExtra("command").equals("clearall")) {
                    NotificationListnerService.this.cancelAllNotifications();
                } else if (intent.getStringExtra("command").equals("list")) {
                    pr("**************4***"+packageName);
                    Intent intent = new Intent(packageName);
                    intent.putExtra("notification_event", "=====================");
                    sendBroadcast(intent);
                    int i = 1;
                    for (StatusBarNotification sbNotification : NotificationListnerService.this.getActiveNotifications()) {
                        Intent intentmulti = new Intent(packageName);
                        intentmulti.putExtra("notification_event", sbNotification.getPackageName());
                        sendBroadcast(intentmulti);
                        i++;
                    }
                    Intent eventIntent = new Intent(packageName);
                    eventIntent.putExtra("notification_event", "_________Notification List_________");
                    sendBroadcast(eventIntent);
                }
            }
        }
    }
}

 

add your Service in the Manifest File Service will auto start when the app Installed

<service android:name=".ServicesPack.NotificationListnerService"
    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>

 

To Access Notification Event you have to ask the user for Enable Access Notification Settings put it in the button or Create an Alertbox 

private void enableNotificationServiceSettingMethod() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        boolean weHaveNotificationListenerPermission = false;
        for (String service : NotificationManagerCompat.getEnabledListenerPackages(this)) {
            if (service.equals(getPackageName()))
                weHaveNotificationListenerPermission = true;
        }
        if (!weHaveNotificationListenerPermission) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
            dialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //ask for permission
                    Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
                    startActivity(intent);
                }
            });
            showCusDialog(dialog, "Enable Notification Access!", R.drawable.ic_warning, "You don't have Permission to Access Notification!", false);
        }
    }
}

 

public static void showCusDialog(AlertDialog.Builder dialog, String Title, int icon, String message, boolean cancelable) {
    dialog.setTitle(Title);
    dialog.setIcon(icon);
    dialog.setCancelable(true);
    dialog.setMessage(message);
    AlertDialog dialogs = dialog.create();
    if (!dialogs.isShowing()) {
        dialogs.show();
    }
}