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(int id) 

Source Link

Document

Cancel a previously shown notification.

Usage

From source file:com.pgmacdesign.rsrtoolbox.FollowUp.java

private void removeNotification() {
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.cancel(uniqueID);
}

From source file:edu.rit.csh.androidwebnews.UpdaterService.java

/**
 * The IntentService calls this method from the default worker thread with
 * the intent that started the service. When this method returns, IntentService
 * stops the service, as appropriate.//from   w w  w.j a va2 s .c  o  m
 */
@Override
protected void onHandleIntent(Intent intent) {
    Vibrator mVibrator;
    Uri notification;
    Ringtone r;
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    HttpsConnector hc = new HttpsConnector(this);
    int[] statuses;
    try {
        statuses = hc.getUnreadCount(); // throws all the errors

        // if there are WebNews new posts and that number is different than last time the update ran
        if (statuses[0] != 0 && statuses[0] != sharedPref.getInt("number_of_unread", 0)) {

            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putInt("number_of_unread", statuses[0]);
            editor.commit();

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setContentTitle(getString(R.string.app_name));
            builder.setSmallIcon(R.drawable.notification_icon);
            builder.setAutoCancel(true);
            if (statuses[2] != 0) {
                if (statuses[2] == 1) {
                    builder.setContentText(statuses[2] + " reply to your post");
                } else {
                    builder.setContentText(statuses[2] + " reply to your posts");
                }
            } else if (statuses[1] != 0) {
                if (statuses[1] == 1) {
                    builder.setContentText(statuses[1] + " unread post in your thread");
                } else {
                    builder.setContentText(statuses[1] + " unread posts in your thread");
                }

            } else {
                if (statuses[0] == 1) {
                    builder.setContentText(statuses[0] + " unread post");
                } else {
                    builder.setContentText(statuses[0] + " unread posts");
                }
            }

            if (sharedPref.getBoolean("vibrate_service", true)) {
                mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                mVibrator.vibrate(500);
            }
            if (sharedPref.getBoolean("ring_service", false)) {
                notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                r.play();
            }

            // Creates an explicit intent for an Activity in your app

            /*
            The stack builder object will contain an artificial back stack for the
            started Activity.
            This ensures that navigating backward from the Activity leads out of
            your application to the Home screen.
            */

            // notification is selected
            Intent notificationIntent = new Intent(this, RecentActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(contentIntent);

            // Add as notification
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(0, builder.build());

        } else if (statuses[0] == 0) { // if all post have been read
            /* if a user reads all the posts, the notification is removed */
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                    Context.NOTIFICATION_SERVICE);
            mNotificationManager.cancel(0);
        }

    } catch (InvalidKeyException e) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setContentTitle(getString(R.string.app_name));
        mBuilder.setSmallIcon(R.drawable.notification_icon);
        mBuilder.setContentText("Invalid API Key");
        mBuilder.setAutoCancel(true);

        Intent resultIntent = new Intent(this, SettingsActivity.class);
        TaskStackBuilder stackBuilder;
        try {
            stackBuilder = TaskStackBuilder.create(this);
        } catch (Exception e1) {
            return;
        }

        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(SettingsActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(0, mBuilder.build());
    } catch (NoInternetException e) {
        // do nothing if there is no internet for the background service
    } catch (InterruptedException e) {
        // normally never hit
    } catch (ExecutionException e) {
        // normally never hit
    }

}

From source file:org.isoron.uhabits.HabitBroadcastReceiver.java

private void dismissNotification(Context context, Long habitId) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Activity.NOTIFICATION_SERVICE);

    int notificationId = (int) (habitId % Integer.MAX_VALUE);
    notificationManager.cancel(notificationId);
}

From source file:fr.vassela.acrrd.notifier.TelephoneCallNotifier.java

public void cancelOngoingNotification(Context context) {
    try {/*from   www  .  j a v  a2 s .c o m*/
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(getOngoingNotificationId());
    } catch (Exception e) {

    }
}

From source file:org.xbmc.kore.service.NotificationObserver.java

private void removeNotification() {
    NotificationManager notificationManager = (NotificationManager) mContext
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(NOTIFICATION_ID);
}

From source file:com.android.systemui.ReminderReceiver.java

@Override
public void onReceive(final Context context, Intent intent) {
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    SharedPreferences shared = context.getSharedPreferences(KEY_REMINDER_ACTION, Context.MODE_PRIVATE);

    if (intent.getBooleanExtra("clear", false)) {
        manager.cancel(NOTI_ID);
        // User has set a new reminder but didn't clear
        // This notification until now
        if (!shared.getBoolean("updated", false)) {
            shared.edit().putBoolean("scheduled", false).commit();
            shared.edit().putInt("hours", -1).commit();
            shared.edit().putInt("minutes", -1).commit();
            shared.edit().putInt("day", -1).commit();
            shared.edit().putString("title", null).commit();
            shared.edit().putString("message", null).commit();
        }//from   w w  w.j a  v  a  2 s  .  c  om
    } else {
        String title = shared.getString("title", null);
        String message = shared.getString("message", null);
        if (title != null && message != null) {
            Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_qs_alarm_on);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setTicker(title)
                    .setContentTitle(title).setContentText(message).setAutoCancel(false).setOngoing(true)
                    .setPriority(NotificationCompat.PRIORITY_HIGH).setSmallIcon(R.drawable.ic_qs_alarm_on)
                    .setLargeIcon(bm).setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(message));

            int alertMode = Settings.System.getIntForUser(context.getContentResolver(),
                    Settings.System.REMINDER_ALERT_NOTIFY, 0, UserHandle.USER_CURRENT);
            PendingIntent result = null;
            Intent serviceIntent = new Intent(context, ReminderService.class);
            if (alertMode != 0 && !QuietHoursHelper.inQuietHours(context, Settings.System.QUIET_HOURS_MUTE)) {
                context.startService(serviceIntent);
            }

            // Stop sound on click
            serviceIntent.putExtra("stopSelf", true);
            result = PendingIntent.getService(context, 1, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            builder.setContentIntent(result);

            // Add button for dismissal
            serviceIntent.putExtra("dismissNoti", true);
            result = PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            builder.addAction(R.drawable.ic_sysbar_null,
                    context.getResources().getString(R.string.quick_settings_reminder_noti_dismiss), result);

            // Add button for reminding later
            serviceIntent.putExtra("time", true);
            result = PendingIntent.getService(context, 2, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            builder.addAction(R.drawable.ic_qs_alarm_on,
                    context.getResources().getString(R.string.quick_settings_reminder_noti_later), result);

            shared.edit().putBoolean("scheduled", false).commit();
            shared.edit().putInt("hours", -1).commit();
            shared.edit().putInt("minutes", -1).commit();
            shared.edit().putInt("day", -1).commit();

            manager.notify(NOTI_ID, builder.build());
        }
    }
    shared.edit().putBoolean("updated", false).commit();
}

From source file:com.concentriclivers.mms.com.android.mms.transaction.MessagingNotification.java

public static void cancelNotification(Context context, int notificationId) {
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    nm.cancel(notificationId);
}

From source file:me.acristoffers.tracker.fragments.PackageDetailsFragment.java

private void setupUI() {
    final Activity activity = getActivity();
    final View view = getView();

    if (activity == null || view == null) {
        return;/*from  w w w  . java  2 s.  com*/
    }

    String code = null;

    final Bundle arguments = getArguments();
    if (arguments != null) {
        code = arguments.getString(PackageDetailsActivity.PACKAGE_CODE);
    }

    if (code == null || code.isEmpty()) {
        final Intent intent = activity.getIntent();
        if (intent != null) {
            code = intent.getStringExtra(PackageDetailsActivity.PACKAGE_CODE);
        }
    }

    if (code == null || code.isEmpty()) {
        return;
    }

    final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.steps);

    final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(activity);
    recyclerView.setLayoutManager(layoutManager);

    pkg = new Package(code, getActivity(), null);

    final StepListAdapter stepListAdapter = new StepListAdapter(pkg, activity);
    recyclerView.setAdapter(stepListAdapter);

    TextView textView = (TextView) view.findViewById(R.id.name);
    textView.setText(pkg.getName());

    textView = (TextView) view.findViewById(R.id.code);
    textView.setText(pkg.getCod());
    textView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            final ClipboardManager clipboardManager = (ClipboardManager) activity
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            if (clipboardManager != null) {
                final String code = pkg.getCod();

                final ClipData clipData = ClipData.newPlainText(code, code);
                clipboardManager.setPrimaryClip(clipData);

                final Toast toast = Toast.makeText(activity, R.string.code_copied, Toast.LENGTH_SHORT);
                toast.show();

                return true;
            }

            return false;
        }
    });

    if (!pkg.getSteps().isEmpty()) {
        textView = (TextView) view.findViewById(R.id.emptyStepView);
        textView.setVisibility(View.INVISIBLE);
    }

    final NotificationManager nm = (NotificationManager) activity
            .getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(pkg.getId());
}

From source file:org.qeo.deviceregistration.ui.NotificationHelper.java

private void remoteDeviceFoundNotification(Context context, Intent intent) {
    LOG.fine("(un)publishing notification for remote device");
    List<RegistrationRequest> unregistered = new ArrayList<RegistrationRequest>();
    for (RegistrationRequest req : RemoteDeviceRegistration.getInstance().getUnregisteredDevices()) {
        RegistrationStatusCode status = UnRegisteredDeviceModel.getRegistrationStatus(req.registrationStatus);
        if (status == RegistrationStatusCode.UNREGISTERED) {
            // only add unregistered devices
            unregistered.add(req);//w w  w  .j av  a2s . c  o  m
        }
    }

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    if (unregistered.isEmpty()) {
        // no more devices in unregistered state, remove notification
        notificationManager.cancel(ServiceApplication.NOTIFICATION_UNREGISTERED_DEVICE_FOUND);
        return;
    }

    // need to create the notification

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.drawable.ic_stat_qeo);
    builder.setContentTitle(context.getString(R.string.unregistered_device_notification_title));
    builder.setContentText(context.getString(R.string.unregistered_device_notification_subtitle));
    builder.setAutoCancel(true);

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, ManagementActivity.class);
    resultIntent.putExtra(ManagementActivity.INTENT_EXTRA_START_TAB, ManagementActivity.TAB_DEVICES);
    if (unregistered.size() == 1) {
        // only 1 device
        UnRegisteredDeviceModel device = new UnRegisteredDeviceModel(unregistered.get(0));
        resultIntent.putExtra(UnRegisteredDeviceListFragment.INTENT_EXTRA_DEVICE_TO_REGISTER, device);
    }

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
            ServiceApplication.REQUEST_CODE_UNREGISTERED_DEVICE_FOUND, PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setContentIntent(resultPendingIntent);

    // mId allows you to update the notification later on.
    notificationManager.notify(ServiceApplication.NOTIFICATION_UNREGISTERED_DEVICE_FOUND, builder.build());

}

From source file:com.android.settings.locationprivacy.LocationPrivacyAdvancedSettings.java

private void addNotification(int id, int textID) {
    String text = getResources().getString(textID);
    Intent intent = new Intent(getActivity(), getActivity().getClass());
    intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, LocationPrivacySettings.class.getName());
    intent.putExtra(PreferenceActivity.EXTRA_NO_HEADERS, true);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);

    Notification noti = new Notification.Builder(getActivity())
            .setContentTitle(getResources().getString(R.string.lp_webservice_notification_title))
            .setSmallIcon(R.drawable.ic_settings_locationprivacy)
            .setStyle(new Notification.BigTextStyle().bigText(text)).setContentIntent(pIntent)
            .setAutoCancel(true).build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            getActivity().NOTIFICATION_SERVICE);
    notificationManager.cancel(WEBSERVICE_ERROR);
    notificationManager.cancel(WEBSERVICE_OK);
    notificationManager.cancel(GOOGLE_PLAY);
    notificationManager.notify(id, noti);
}