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.money.manager.ex.notifications.SmsReceiverTransactions.java

/**
 * Note: Check the new NotificationUtils for creation of notification channel and the code that
 * utilizes it.//from   ww w . j  a v  a 2  s.  c  o  m
 * @param intent
 * @param notificationText
 */
private void showNotification(Intent intent, String notificationText) {

    int NOTIFICATION_ID = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);

    String NOTIFICATION_CHANNEL_ID = "ammex_" + String.valueOf(NOTIFICATION_ID); // The id of the channel.

    intent.putExtra("NOTIFICATION_ID", NOTIFICATION_ID);

    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 1910, intent, 0);

    // Gets an instance of the NotificationManager service
    NotificationManager mNotificationManager = (NotificationManager) mContext
            .getSystemService(Context.NOTIFICATION_SERVICE);

    //Get an instance of NotificationManager//
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_stat_notification)
            .setContentTitle(mContext.getString(R.string.application_name) + " - SMS Auto Transaction Failed")
            .setContentText(notificationText)
            .addAction(R.drawable.ic_action_folder_open_dark, "Edit", pendingIntent)
            .setContentIntent(pendingIntent).setPriority(Notification.PRIORITY_MAX).setNumber(NOTIFICATION_ID)
            .setAutoCancel(false);

    mNotificationManager.cancel(NOTIFICATION_ID);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

From source file:im.neon.services.EventStreamService.java

/**
 * Hide the permanent call notifications
 *///www .jav  a  2  s .  c  om
public void hideCallNotifications() {
    NotificationManager nm = (NotificationManager) EventStreamService.this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    // hide the call
    if ((FOREGROUND_NOTIF_ID_PENDING_CALL == mForegroundServiceIdentifier)
            || (FOREGROUND_ID_INCOMING_CALL == mForegroundServiceIdentifier)) {
        if (FOREGROUND_NOTIF_ID_PENDING_CALL == mForegroundServiceIdentifier) {
            mCallIdInProgress = null;
        } else {
            mIncomingCallId = null;
        }
        nm.cancel(NOTIF_ID_FOREGROUND_SERVICE);
        mForegroundServiceIdentifier = -1;
        stopForeground(true);
        updateServiceForegroundState();
    }
}

From source file:com.krayzk9s.imgurholo.services.UploadService.java

public void onGetObject(Object o, String tag) {
    String id = (String) o;
    if (id.length() == 7) {
        if (totalUpload != -1)
            ids.add(id);//from w ww .j  a va 2s.  c o  m
        if (ids.size() == totalUpload) {
            ids.add(0, ""); //weird hack because imgur eats the first item of the array for some bizarre reason
            NewAlbumAsync newAlbumAsync = new NewAlbumAsync("", "", apiCall, ids, this);
            newAlbumAsync.execute();
        }
    } else if (apiCall.settings.getBoolean("AlbumUpload", true)) {
        NotificationManager notificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        Intent viewImageIntent = new Intent();
        viewImageIntent.setAction(Intent.ACTION_VIEW);
        viewImageIntent.setData(Uri.parse("http://imgur.com/a/" + id));
        Intent shareIntent = new Intent();
        shareIntent.setType("text/plain");
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        shareIntent.putExtra(Intent.EXTRA_TEXT, "http://imgur.com/a/" + id);
        PendingIntent viewImagePendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(),
                viewImageIntent, 0);
        PendingIntent sharePendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(),
                shareIntent, 0);
        Notification notification = notificationBuilder.setSmallIcon(R.drawable.icon_desaturated)
                .setContentText("Finished Uploading Album").setContentTitle("imgur Image Uploader")
                .setContentIntent(viewImagePendingIntent)
                .addAction(R.drawable.dark_social_share, "Share", sharePendingIntent).build();
        Log.d("Built", "Notification built");
        notificationManager.cancel(0);
        notificationManager.notify(1, notification);
        Log.d("Built", "Notification display");
    } else {
        NotificationManager notificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        Notification notification = notificationBuilder.setSmallIcon(R.drawable.icon_desaturated)
                .setContentText("Finished Uploading All Images").setContentTitle("imgur Image Uploader")
                .build();
        Log.d("Built", "Notification built");
        notificationManager.cancel(0);
        notificationManager.notify(1, notification);
        Log.d("Built", "Notification display");
    }
}

From source file:com.klinker.deskclock.alarms.AlarmNotifications.java

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

    // Close dialogs and window shade, so this will display
    context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

    Resources resources = context.getResources();
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentTitle(instance.getLabelOrDefault(context))
            .setContentText(AlarmUtils.getFormattedTime(context, instance.getAlarmTime()))
            .setSmallIcon(R.drawable.stat_notify_alarm).setOngoing(true).setAutoCancel(false).setWhen(0);

    // Setup Snooze Action
    Intent snoozeIntent = AlarmStateManager.createStateChangeIntent(context, "SNOOZE_TAG", instance,
            AlarmInstance.SNOOZE_STATE);
    PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(context, instance.hashCode(), snoozeIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(R.drawable.stat_notify_alarm, resources.getString(R.string.alarm_alert_snooze_text),
            snoozePendingIntent);//w  w  w .  ja  v  a  2  s  .c om

    // Setup Dismiss Action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context, "DISMISS_TAG", instance,
            AlarmInstance.DISMISSED_STATE);
    PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(context, instance.hashCode(), dismissIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(android.R.drawable.ic_menu_close_clear_cancel,
            resources.getString(R.string.alarm_alert_dismiss_text), dismissPendingIntent);

    // Setup Content Action
    Intent contentIntent = AlarmInstance.createIntent(context, AlarmActivity.class, instance.mId);
    notification.setContentIntent(PendingIntent.getActivity(context, instance.hashCode(), contentIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup fullscreen intent
    Intent fullScreenIntent = AlarmInstance.createIntent(context, AlarmActivity.class, instance.mId);
    // set action, so we can be different then content pending intent
    fullScreenIntent.setAction("fullscreen_activity");
    fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    notification.setFullScreenIntent(PendingIntent.getActivity(context, instance.hashCode(), fullScreenIntent,
            PendingIntent.FLAG_UPDATE_CURRENT), true);
    notification.setPriority(NotificationCompat.PRIORITY_MAX);

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

From source file:dev.ukanth.ufirewall.MainActivity.java

private void reloadPreferences() {

    getSupportActionBar().setDisplayShowHomeEnabled(true);

    G.reloadPrefs();/*  w  w w. j  a  va  2s . c om*/
    checkPreferences();
    //language
    Api.updateLanguage(getApplicationContext(), G.locale());

    if (this.listview == null) {
        this.listview = (ListView) this.findViewById(R.id.listview);
    }

    //verifyMultiProfile();
    refreshHeader();
    updateIconStatus();

    NotificationManager mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel(Api.NOTIFICATION_ID);

    if (G.disableIcons()) {
        this.findViewById(R.id.imageHolder).setVisibility(View.GONE);
    } else {
        this.findViewById(R.id.imageHolder).setVisibility(View.VISIBLE);
    }

    if (G.showFilter()) {
        this.findViewById(R.id.filerOption).setVisibility(View.VISIBLE);
    } else {
        this.findViewById(R.id.filerOption).setVisibility(View.GONE);
    }

    if (G.enableMultiProfile()) {
        this.findViewById(R.id.profileOption).setVisibility(View.VISIBLE);
    } else {
        this.findViewById(R.id.profileOption).setVisibility(View.GONE);
    }
    if (G.enableRoam()) {
        addColumns(R.id.img_roam);
    } else {
        hideColumns(R.id.img_roam);
    }
    if (G.enableVPN()) {
        addColumns(R.id.img_vpn);
    } else {
        hideColumns(R.id.img_vpn);
    }

    if (!Api.isMobileNetworkSupported(getApplicationContext())) {
        ImageView view = (ImageView) this.findViewById(R.id.img_3g);
        view.setVisibility(View.GONE);

    } else {
        this.findViewById(R.id.img_3g).setOnClickListener(this);
    }

    if (G.enableLAN()) {
        addColumns(R.id.img_lan);
    } else {
        hideColumns(R.id.img_lan);
    }

    updateRadioFilter();
    if (G.enableMultiProfile()) {
        setupMultiProfile(true);
    }

    selectFilterGroup();
}

From source file:com.autoupdateapk.AutoUpdateApk.java

protected void raise_notification() {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager) context.getSystemService(ns);

    // nm.cancel( NOTIFICATION_ID ); // tried this, but it just doesn't do
    // the trick =(
    nm.cancelAll();/*w w  w .ja  v a  2 s.co  m*/

    String update_file = preferences.getString(UPDATE_FILE, "");
    if (update_file.length() > 0) {
        setChanged();
        notifyObservers(AUTOUPDATE_HAVE_UPDATE);

        // raise notification
        Notification notification = new Notification(appIcon, appName + " update", System.currentTimeMillis());
        notification.flags |= NOTIFICATION_FLAGS;

        CharSequence contentTitle = appName + " update available";
        CharSequence contentText = "Select to install";
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setDataAndType(
                Uri.parse("file://" + context.getFilesDir().getAbsolutePath() + "/" + update_file),
                ANDROID_PACKAGE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        nm.notify(NOTIFICATION_ID, notification);
    } else {
        nm.cancel(NOTIFICATION_ID);
    }
}

From source file:com.deepak.myclock.alarms.AlarmNotifications.java

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

    // Close dialogs and window shade, so this will display
    context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

    Resources resources = context.getResources();
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentTitle(instance.getLabelOrDefault(context))
            .setContentText(AlarmUtils.getFormattedTime(context, instance.getAlarmTime()))
            .setSmallIcon(R.drawable.stat_notify_alarm).setOngoing(true).setAutoCancel(false).setWhen(0);

    // Setup Snooze Action
    Intent snoozeIntent = AlarmStateManager.createStateChangeIntent(context, "SNOOZE_TAG", instance,
            AlarmInstance.SNOOZE_STATE);
    PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(context, instance.hashCode(), snoozeIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(R.drawable.stat_notify_alarm, resources.getString(R.string.alarm_alert_snooze_text),
            snoozePendingIntent);//from w w w.j a  v  a2  s  .co m

    // Setup Dismiss Action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context, "DISMISS_TAG", instance,
            AlarmInstance.DISMISSED_STATE);
    PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(context, instance.hashCode(), dismissIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(android.R.drawable.ic_menu_close_clear_cancel,
            resources.getString(R.string.alarm_alert_dismiss_text), dismissPendingIntent);

    // Setup Content Action
    Intent contentIntent = AlarmInstance.createIntent(context, AlarmActivity.class, instance.mId);
    notification.setContentIntent(PendingIntent.getActivity(context, instance.hashCode(), contentIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup fullscreen intent
    /*Intent fullScreenIntent = AlarmInstance.createIntent(context, AlarmActivity.class,
       instance.mId);
    // set action, so we can be different then content pending intent
    fullScreenIntent.setAction("fullscreen_activity");
    fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
       Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    notification.setFullScreenIntent(PendingIntent.getActivity(context,
       instance.hashCode(), fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT), true);*/
    notification.setPriority(NotificationCompat.PRIORITY_MAX);

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

From source file:com.money.manager.ex.notifications.RepeatingTransactionNotifications.java

public void notifyRepeatingTransaction() {
    // create application
    CurrencyUtils currencyUtils = new CurrencyUtils(context);
    // init currencies
    if (!currencyUtils.isInit())
        currencyUtils.init();//from w w  w .j  a va  2 s.c  o m

    // select data
    QueryBillDeposits billDeposits = new QueryBillDeposits(context);
    MoneyManagerOpenHelper databaseHelper = new MoneyManagerOpenHelper(context);

    if (databaseHelper != null) {
        Cursor cursor = databaseHelper.getReadableDatabase().rawQuery(billDeposits.getSource() + " AND "
                + QueryBillDeposits.DAYSLEFT + "<=0 ORDER BY " + QueryBillDeposits.NEXTOCCURRENCEDATE, null);
        if (cursor != null) {
            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
                while (!cursor.isAfterLast()) {
                    String line = cursor
                            .getString(cursor.getColumnIndex(QueryBillDeposits.USERNEXTOCCURRENCEDATE)) + " "
                            + cursor.getString(cursor.getColumnIndex(QueryBillDeposits.PAYEENAME)) + ": <b>"
                            + currencyUtils.getCurrencyFormatted(
                                    cursor.getInt(cursor.getColumnIndex(QueryBillDeposits.CURRENCYID)),
                                    cursor.getDouble(cursor.getColumnIndex(QueryBillDeposits.AMOUNT)))
                            + "</b>";
                    // add line
                    inboxStyle.addLine(Html.fromHtml("<small>" + line + "</small>"));
                    // move to next row
                    cursor.moveToNext();
                }

                NotificationManager notificationManager = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                // create pendig intent
                Intent intent = new Intent(context, RepeatingTransactionListActivity.class);
                // set launch from notification // check pin code
                intent.putExtra(RepeatingTransactionListActivity.INTENT_EXTRA_LAUNCH_NOTIFICATION, true);

                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
                // create notification
                Notification notification = null;
                try {
                    notification = new NotificationCompat.Builder(context).setAutoCancel(true)
                            .setContentIntent(pendingIntent)
                            .setContentTitle(context.getString(R.string.application_name))
                            .setContentText(
                                    context.getString(R.string.notification_repeating_transaction_expired))
                            .setSubText(context
                                    .getString(R.string.notification_click_to_check_repeating_transaction))
                            .setSmallIcon(R.drawable.ic_stat_notification)
                            .setTicker(context.getString(R.string.notification_repeating_transaction_expired))
                            .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND
                                    | Notification.DEFAULT_LIGHTS)
                            .setNumber(cursor.getCount()).setStyle(inboxStyle).build();
                    // notify 
                    notificationManager.cancel(ID_NOTIFICATION);
                    notificationManager.notify(ID_NOTIFICATION, notification);
                } catch (Exception e) {
                    Log.e(LOGCAT, e.getMessage());
                }
            }
            // close cursor
            cursor.close();
        }
        // close database helper
        //databaseHelper.close();
    }
}

From source file:com.xorcode.andtweet.AndTweetService.java

/**
 * Notify user of the commands Queue size
 * // ww  w. j  a v  a 2s .  c o m
 * @return total size of Queues
 */
private int notifyOfQueue(boolean clearNotification) {
    int count = mRetryQueue.size() + mCommands.size();
    NotificationManager nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if (count == 0 || clearNotification) {
        // Clear notification
        nM.cancel(CommandEnum.NOTIFY_QUEUE.ordinal());
    } else if (mNotificationsEnabled) {
        if (mRetryQueue.size() > 0) {
            MyLog.d(TAG, mRetryQueue.size() + " commands in Retry Queue.");
        }
        if (mCommands.size() > 0) {
            MyLog.d(TAG, mCommands.size() + " commands in Main Queue.");
        }

        // Set up the notification to display to the user
        Notification notification = new Notification(R.drawable.notification_icon,
                (String) getText(R.string.notification_title), System.currentTimeMillis());

        int messageTitle;
        String aMessage = "";

        aMessage = I18n.formatQuantityMessage(getApplicationContext(), R.string.notification_queue_format,
                count, R.array.notification_queue_patterns, R.array.notification_queue_formats);
        messageTitle = R.string.notification_title_queue;

        // Set up the scrolling message of the notification
        notification.tickerText = aMessage;

        /**
         * Set the latest event information and send the notification
         * Actually don't start any intent
         * 
         * @see http
         *      ://stackoverflow.com/questions/4232006/android-notification
         *      -pendingintent-problem
         */
        // PendingIntent pi = PendingIntent.getActivity(this, 0, null, 0);

        /**
         * Kick the commands queue by sending empty command
         */
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, new CommandData(CommandEnum.EMPTY).toIntent(),
                0);

        notification.setLatestEventInfo(this, getText(messageTitle), aMessage, pi);
        nM.notify(CommandEnum.NOTIFY_QUEUE.ordinal(), notification);
    }
    return count;
}

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

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

    // Close dialogs and window shade, so this will display
    context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

    Resources resources = context.getResources();
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentTitle(instance.getLabelOrDefault(context))
            .setContentText(AlarmUtils.getFormattedTime(context, instance.getAlarmTime()))
            .setSmallIcon(R.drawable.stat_notify_alarm).setOngoing(true).setAutoCancel(false)
            .setDefaults(NotificationCompat.DEFAULT_LIGHTS).setWhen(0)
            .setCategory(NotificationCompat.CATEGORY_ALARM);

    // Setup Snooze Action
    Intent snoozeIntent = AlarmStateManager.createStateChangeIntent(context, "SNOOZE_TAG", instance,
            AlarmInstance.SNOOZE_STATE);
    PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(context, instance.hashCode(), snoozeIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(R.drawable.stat_notify_alarm, resources.getString(R.string.alarm_alert_snooze_text),
            snoozePendingIntent);// w w w . j av  a  2s  .co m

    // Setup Dismiss Action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context, "DISMISS_TAG", instance,
            AlarmInstance.DISMISSED_STATE);
    PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(context, instance.hashCode(), dismissIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(android.R.drawable.ic_menu_close_clear_cancel,
            resources.getString(R.string.alarm_alert_dismiss_text), dismissPendingIntent);

    // Setup Content Action
    Intent contentIntent = AlarmInstance.createIntent(context, AlarmActivity.class, instance.mId);
    notification.setContentIntent(PendingIntent.getActivity(context, instance.hashCode(), contentIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup fullscreen intent
    Intent fullScreenIntent = AlarmInstance.createIntent(context, AlarmActivity.class, instance.mId);
    // set action, so we can be different then content pending intent
    fullScreenIntent.setAction("fullscreen_activity");
    fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    notification.setFullScreenIntent(PendingIntent.getActivity(context, instance.hashCode(), fullScreenIntent,
            PendingIntent.FLAG_UPDATE_CURRENT), true);
    notification.setPriority(NotificationCompat.PRIORITY_MAX);

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