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.google.samples.apps.iosched.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LOGD(TAG, "Not scheduling alarm because target time is in the past: " + sessionStart);
        return;//from   w w w. j  a  va  2s.co  m
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    LOGD(TAG, "Scheduling alarm for " + alarmTime + " = " + (new Date(alarmTime)).toString());

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(new Uri.Builder().authority("com.google.samples.apps.iosched")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LOGD(TAG, "-> Intent extra: session alarm offset " + alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.meetingcpp.sched.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LOGD(TAG, "Not scheduling alarm because target time is in the past: " + sessionStart);
        return;//from w  w  w . j  av a  2 s .co m
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    LOGD(TAG, "Scheduling alarm for " + alarmTime + " = " + (new Date(alarmTime)).toString());

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(
            new Uri.Builder().authority("com.meetingcpp.sched").path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LOGD(TAG, "-> Intent extra: session alarm offset " + alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.razza.apps.iosched.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LogUtils.LOGD(TAG, "Not scheduling alarm because target time is in the past: " + sessionStart);
        return;/*from  w w w  .  j ava2s .  c  o  m*/
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    LogUtils.LOGD(TAG, "Scheduling alarm for " + alarmTime + " = " + (new Date(alarmTime)).toString());

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(
            new Uri.Builder().authority("com.razza.apps.iosched").path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LogUtils.LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LogUtils.LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LogUtils.LOGD(TAG, "-> Intent extra: session alarm offset " + alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    LogUtils.LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.embeddedlog.LightUpDroid.alarms.AlarmNotifications.java

public static void showMissedNotification(Context context, AlarmInstance instance) {
    Log.v("Displaying missed notification for alarm instance: " + instance.mId);
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    String label = instance.mLabel;
    String alarmTime = AlarmUtils.getFormattedTime(context, instance.getAlarmTime());
    String contextText = instance.mLabel.isEmpty() ? alarmTime
            : context.getString(R.string.alarm_missed_text, alarmTime, label);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentTitle(context.getString(R.string.alarm_missed_title)).setContentText(contextText)
            .setSmallIcon(R.drawable.stat_notify_alarm).setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM);

    // Setup dismiss intent
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context, "DISMISS_TAG", instance,
            AlarmInstance.DISMISSED_STATE);
    notification.setDeleteIntent(PendingIntent.getBroadcast(context, instance.hashCode(), dismissIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup content intent
    Intent showAndDismiss = AlarmInstance.createIntent(context, AlarmStateManager.class, instance.mId);
    showAndDismiss.setAction(AlarmStateManager.SHOW_AND_DISMISS_ALARM_ACTION);
    notification.setContentIntent(PendingIntent.getBroadcast(context, instance.hashCode(), showAndDismiss,
            PendingIntent.FLAG_UPDATE_CURRENT));

    nm.cancel(instance.hashCode());
    nm.notify(instance.hashCode(), notification.build());
}

From source file:com.lambdasoup.quickfit.alarm.AlarmService.java

@WorkerThread
private void refreshNotificationDisplay() {
    try (Cursor toNotify = getContentResolver().query(QuickFitContentProvider.getUriWorkoutsList(),
            new String[] { WorkoutEntry.SCHEDULE_ID, WorkoutEntry.WORKOUT_ID, WorkoutEntry.ACTIVITY_TYPE,
                    WorkoutEntry.LABEL, WorkoutEntry.DURATION_MINUTES },
            ScheduleEntry.TABLE_NAME + "." + ScheduleEntry.COL_SHOW_NOTIFICATION + "=?",
            new String[] { Integer.toString(ScheduleEntry.SHOW_NOTIFICATION_YES) }, null)) {
        int count = toNotify == null ? 0 : toNotify.getCount();
        if (count == 0) {
            Timber.d("refreshNotificationDisplay: no events");
            NotificationManager notificationManager = (NotificationManager) getSystemService(
                    NOTIFICATION_SERVICE);
            notificationManager.cancel(Constants.NOTIFICATION_ALARM);
            return;
        }//from   w w w  .j  ava 2s  . c  o m

        long[] scheduleIds = new long[count];
        int i = 0;
        toNotify.moveToPosition(-1);
        while (toNotify.moveToNext()) {
            scheduleIds[i] = toNotify.getLong(toNotify.getColumnIndex(WorkoutEntry.SCHEDULE_ID));
            i++;
        }

        PendingIntent cancelIntent = PendingIntent.getService(getApplicationContext(), 0,
                getIntentOnNotificationsCanceled(this, scheduleIds), PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notification;
        if (count == 1) {
            Timber.d("refreshNotificationDisplay: single event");
            toNotify.moveToFirst();
            notification = notifySingleEvent(toNotify, cancelIntent);
        } else {
            Timber.d("refreshNotificationDisplay: multiple events");
            toNotify.moveToPosition(-1);
            notification = notifyMultipleEvents(toNotify, cancelIntent);
        }

        notification.setDeleteIntent(cancelIntent);
        notification.setAutoCancel(true);
        notification.setPriority(Notification.PRIORITY_HIGH);
        notification.setSmallIcon(R.drawable.ic_stat_quickfit_icon);
        notification.setColor(ContextCompat.getColor(this, R.color.colorPrimary));

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String ringtoneUriStr = preferences.getString(getString(R.string.pref_key_notification_ringtone), null);
        if (ringtoneUriStr == null) {
            notification.setSound(
                    RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION));
        } else if (!ringtoneUriStr.isEmpty()) {
            notification.setSound(Uri.parse(ringtoneUriStr));
        }
        boolean ledOn = preferences.getBoolean(getString(R.string.pref_key_notification_led), true);
        boolean vibrationOn = preferences.getBoolean(getString(R.string.pref_key_notification_vibrate), true);
        notification.setDefaults(
                (ledOn ? Notification.DEFAULT_LIGHTS : 0) | (vibrationOn ? Notification.DEFAULT_VIBRATE : 0));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notification.setCategory(Notification.CATEGORY_ALARM);
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(Constants.NOTIFICATION_ALARM, notification.build());
    }
}

From source file:com.atlas.mars.weatherradar.MainActivity.java

@Override
public void onNewIntent(Intent intent) {
    Bundle extras = intent.getExtras();/*from   w w  w.ja  v  a 2  s .  c o  m*/
    if (extras != null) {

        if (extras.containsKey("dist")) {
            mapFragments.get(0).reloadImg();
            show(extras.getInt("dist") + " km");
            isFromNotification = true;
            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            nm.cancel(1);
        }

        if (extras.containsKey("time")) {
            if (forecast != null) {
                forecast.onRegen();
            } else {
                forecast = new Forecast(this, forecastLinearLayout);
            }
            show(Cities.getStringResourceByName("probability_rain", this) + " " + extras.getString("time")
                    + Cities.getStringResourceByName("hh", this));
            isFromNotification = true;

        }
    }
}

From source file:bucci.dev.freestyle.TimerActivity.java

@Override
protected void onResume() {
    if (DEBUG)/*  w w w  . j  av  a  2  s  . com*/
        Log.d(TAG, "onResume()");
    LocalBroadcastManager.getInstance(this).registerReceiver(mMsgReceiver, new TimerIntentFilter());
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel(NotificationCreator.NOTIFICATION_TIMER_RUNNING);

    super.onResume();
}

From source file:com.android.mms.quickmessage.QuickMessage.java

private void clearNotification(boolean markAsRead) {
    // Dismiss the notification that brought us here.
    mCurrentQm = mMessageList.get(mCurrentQmIndex);
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(mCurrentQm.getNotificationId());

    // Mark all contained conversations as seen
    if (markAsRead) {
        markAllMessagesRead();//from   w  w  w .  j av a2  s  . c  om
    }

    // Clear the messages list
    mMessageList.clear();
}

From source file:org.gluu.super_gluu.app.GluuMainActivity.java

public void doQrRequest(OxPush2Request oxPush2Request) {
    if (!validateOxPush2Request(oxPush2Request)) {
        return;//w w w.  java  2s  .co  m
    }
    Settings.setPushDataEmpty(getApplicationContext());
    NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(GluuMainActivity.MESSAGE_NOTIFICATION_ID);
    final ProcessManager processManager = createProcessManager(oxPush2Request);
    ApproveDenyFragment approveDenyFragment = new ApproveDenyFragment();
    approveDenyFragment.setIsUserInfo(false);
    approveDenyFragment.setPush2Request(oxPush2Request);
    approveDenyFragment.setListener(new RequestProcessListener() {
        @Override
        public void onApprove() {
            processManager.onOxPushRequest(false);
            //Show tab again
            tabLayout.getLayoutParams().height = LinearLayout.LayoutParams.WRAP_CONTENT;
        }

        @Override
        public void onDeny() {
            processManager.onOxPushRequest(true);
            //Show tab again
            tabLayout.getLayoutParams().height = LinearLayout.LayoutParams.WRAP_CONTENT;
        }
    });
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.main_root_frame, approveDenyFragment);
    transaction.addToBackStack(null);
    transaction.commitAllowingStateLoss();
    //To hide tabs
    tabLayout.getLayoutParams().height = 0;
}

From source file:com.orange.oidc.secproxy_service.Service.java

private void hideNotifIcon() {
    Logd(TAG, "hideNotifIcon");

    if (Context.NOTIFICATION_SERVICE != null) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
        nMgr.cancel(0);
    }/* w ww  .  j av a  2 s.c  om*/
}