Example usage for android.app NotificationManager cancel

List of usage examples for android.app NotificationManager cancel

Introduction

In this page you can find the example usage for android.app NotificationManager cancel.

Prototype

public void cancel(String tag, int id) 

Source Link

Document

Cancel a previously shown notification.

Usage

From source file:com.android.madpausa.cardnotificationviewer.ConcreteNotificationListenerService.java

/**
 * sends the service notification//from  w ww. j  a v a2 s  . c  o m
 */
private void handleServiceNotification() {

    NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    //filtering archived notification list
    List<StatusBarNotification> filteredArchivedNotificationList = baseNotificationFilter
            .applyFilter(archivedNotificationMap.values(), notificationGroups, true);
    int filteredArchiveddSize = filteredArchivedNotificationList.size();

    //should show notification only if there are notifications to be shown
    if (filteredArchiveddSize > 0) {
        NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);

        nBuilder.setContentTitle(
                String.format(getString(R.string.service_notification_text), filteredArchiveddSize));

        nBuilder.setSmallIcon(R.drawable.ic_notification);
        //gets the correct color resource, based on android version
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            nBuilder.setColor(getResources().getColor(R.color.app_background, null));
        else //noinspection deprecation
            nBuilder.setColor(getResources().getColor(R.color.app_background));

        //setting the intent
        Intent resultIntent = new Intent(this, MainActivity.class);

        //setting the extra containing the archived notifications
        resultIntent.putExtra(ARCHIVED_NOTIFICATIONS_EXTRA, new HashSet<>(archivedNotificationMap.keySet()));

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        nBuilder.setContentIntent(resultPendingIntent);

        //low priority, not min, as it has to show in the lockscreen
        nBuilder.setPriority(Notification.PRIORITY_LOW);

        Notification notification = nBuilder.build();

        //this notification should be sticky
        notification.flags |= Notification.FLAG_NO_CLEAR;
        nManager.notify(SERVICE_NOTIFICATION, 0, notification);
    }
    //else I should remove the notification
    else
        nManager.cancel(SERVICE_NOTIFICATION, 0);
}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public void dismissNotification(Object o) {
    NotificationManager notificationManager = (NotificationManager) getContext()
            .getSystemService(Activity.NOTIFICATION_SERVICE);
    if (o != null) {
        Integer n = (Integer) o;
        notificationManager.cancel("CN1", n.intValue());
    } else {//from w  w w .j  av a  2 s  .co  m
        notificationManager.cancelAll();
    }
}