Example usage for android.app Notification FLAG_AUTO_CANCEL

List of usage examples for android.app Notification FLAG_AUTO_CANCEL

Introduction

In this page you can find the example usage for android.app Notification FLAG_AUTO_CANCEL.

Prototype

int FLAG_AUTO_CANCEL

To view the source code for android.app Notification FLAG_AUTO_CANCEL.

Click Source Link

Document

Bit to be bitwise-ored into the #flags field that should be set if the notification should be canceled when it is clicked by the user.

Usage

From source file:com.amazonaws.mobileconnectors.pinpoint.targeting.notification.NotificationClient.java

private boolean displayNotification(final Bundle pushBundle, final Class<?> targetClass, String imageUrl,
        String iconImageUrl, String iconSmallImageUrl, Map<String, String> campaignAttributes,
        String intentAction) {/*from   w  ww.j a va 2s .  co m*/
    log.info("Display Notification: " + pushBundle.toString());

    final String title = pushBundle.getString(NOTIFICATION_TITLE_PUSH_KEY);
    final String message = pushBundle.getString(NOTIFICATION_BODY_PUSH_KEY);

    final String campaignId = campaignAttributes.get(CAMPAIGN_ID_ATTRIBUTE_KEY);
    final String activityId = campaignAttributes.get(CAMPAIGN_ACTIVITY_ID_ATTRIBUTE_KEY);

    final int requestID = (campaignId + ":" + activityId + ":" + System.currentTimeMillis()).hashCode();

    final int iconResId = getNotificationIconResourceId(pushBundle.getString(NOTIFICATION_ICON_PUSH_KEY));
    if (iconResId == 0) {
        return false;
    }

    final Notification notification = createNotification(iconResId, title, message, imageUrl, iconImageUrl,
            iconSmallImageUrl,
            this.createOpenAppPendingIntent(pushBundle, targetClass, campaignId, requestID, intentAction));

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;

    if (android.os.Build.VERSION.SDK_INT >= ANDROID_LOLLIPOP) {
        log.info("SDK greater than 21 detected: " + android.os.Build.VERSION.SDK_INT);

        final String colorString = pushBundle.getString(NOTIFICATION_COLOR_PUSH_KEY);
        if (colorString != null) {
            int color;
            try {
                color = Color.parseColor(colorString);
            } catch (final IllegalArgumentException ex) {
                log.warn("Couldn't parse campaign notification color.", ex);
                color = 0;
            }
            Exception exception = null;
            try {
                final Field colorField = notification.getClass().getDeclaredField("color");
                colorField.setAccessible(true);
                colorField.set(notification, color);
            } catch (final IllegalAccessException ex) {
                exception = ex;
            } catch (final NoSuchFieldException ex) {
                exception = ex;
            }
            if (exception != null) {
                log.error("Couldn't set campaign notification color : " + exception.getMessage(), exception);
            }
        }
    }

    final NotificationManager notificationManager = (NotificationManager) pinpointContext
            .getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(requestID, notification);
    return true;
}

From source file:jp.co.conit.sss.sp.ex1.billing.BillingService.java

/**
 * ????/*from  w w  w .j a  va 2s .c om*/
 */
private void notificationRestore() {
    Context context = getApplicationContext();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification();
    Intent intent = new Intent();
    PendingIntent pendingintent = PendingIntent.getActivity(context, 0, intent, 0);

    notification.icon = R.drawable.ic_status;
    notification.tickerText = context.getString(R.string.notification_restore);
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(context, context.getString(R.string.notification_restore),
            context.getString(R.string.notification_urge_download), pendingintent);
    notificationManager.notify(1, notification);

}

From source file:net.ben.subsonic.androidapp.util.Util.java

public static void showErrorNotification(final Context context, Handler handler, String title,
        Exception error) {//  w w  w  . j  a va 2s.  c om
    final NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    StringBuilder text = new StringBuilder();
    if (error.getMessage() != null) {
        text.append(error.getMessage()).append(" (");
    }
    text.append(error.getClass().getSimpleName());
    if (error.getMessage() != null) {
        text.append(")");
    }

    // Set the icon, scrolling text and timestamp
    final Notification notification = new Notification(android.R.drawable.stat_sys_warning, title,
            System.currentTimeMillis());
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // The PendingIntent to launch our activity if the user selects this notification
    Intent intent = new Intent(context, ErrorActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(Constants.INTENT_EXTRA_NAME_ERROR, title + ".\n\n" + text);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, text, contentIntent);

    // Send the notification.
    handler.post(new Runnable() {
        @Override
        public void run() {
            notificationManager.cancel(Constants.NOTIFICATION_ID_ERROR);
            notificationManager.notify(Constants.NOTIFICATION_ID_ERROR, notification);
        }
    });
}

From source file:jp.co.conit.sss.sp.ex1.billing.BillingService.java

/**
 * ????/*w  w  w  .jav  a2 s. c om*/
 * 
 * @param purchases
 */
private void notificationVerifiedProduct(List<VerifiedProduct> purchases) {

    Context context = getApplicationContext();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    HashMap<String, VerifiedProduct> orderMap = new HashMap<String, VerifiedProduct>();
    int size = purchases.size() - 1;

    // ????Map???????????
    for (int i = size; i >= 0; i--) {
        VerifiedProduct vp = purchases.get(i);
        if (vp.getPurchaseState() == PurchaseState.PURCHASED) {
            orderMap.put(vp.getProductId(), vp);
        }
    }

    // ?????Notification??
    // ?????
    if (orderMap.size() > 0) {

        Notification notification = new Notification();
        notification.icon = R.drawable.ic_status;
        notification.tickerText = context.getString(R.string.notification_buy_result);
        notification.flags = Notification.FLAG_AUTO_CANCEL;

        Intent intent = new Intent();
        PendingIntent pendingintent = PendingIntent.getActivity(context, 0, intent, 0);

        String title = context.getString(R.string.notification_book);

        notification.setLatestEventInfo(context, context.getString(R.string.notification_buy_finish, title),
                context.getString(R.string.notification_urge_download), pendingintent);

        notificationManager.notify(0, notification);
    }

}

From source file:com.googlecode.android_scripting.facade.AndroidFacade.java

@Rpc(description = "Displays a notification that will be canceled when the user clicks on it.")
public void notify(@RpcParameter(name = "title", description = "title") String title,
        @RpcParameter(name = "message") String message) {
    Notification notification = new Notification(mResources.getLogo48(), message, System.currentTimeMillis());
    // This contentIntent is a noop.
    PendingIntent contentIntent = PendingIntent.getService(mService, 0, new Intent(), 0);
    notification.setLatestEventInfo(mService, title, message, contentIntent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;

    // Get a unique notification id from the application.
    final int notificationId = NotificationIdFactory.create();
    mNotificationManager.notify(notificationId, notification);
}

From source file:org.croudtrip.gcm.GcmIntentService.java

private void createNotification(String title, String message, int notificationId, PendingIntent contentIntent) {
    NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_directions_car_white).setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message)).setContentText(message)
            .setContentIntent(contentIntent).build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(notificationId, notification);
}

From source file:com.roamprocess1.roaming4world.syncadapter.SyncAdapter.java

private void showNotification(String count, String number_date, int j) {

    //   Notification notification = new Notification(R.drawable.r4wlogowhitefill,count, System.currentTimeMillis());
    Intent in = new Intent(mcontext, SipHome.class);
    in.putExtra("MissedCall", true);
    PendingIntent contentIntent = PendingIntent.getActivity(mcontext, 0, in, PendingIntent.FLAG_CANCEL_CURRENT);
    //      notification.setLatestEventInfo(mcontext, count, number_date, contentIntent);
    Notification notification = new NotificationCompat.Builder(mcontext).setContentTitle(count)
            .setContentText(number_date).setSmallIcon(R.drawable.r4wlogowhitefill)
            //                                         .setGroup(GROUP_KEY_MISSEDCALLS)
            .setContentIntent(contentIntent).build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(j, notification);

}

From source file:de.ub0r.android.websms.connector.common.Utils.java

/**
 * Show update notification.//www . jav  a 2s  . c o  m
 * 
 * @param context
 *            {@link Context}
 * @param pkg
 *            package
 */
public static void showUpdateNotification(final Context context, final String pkg) {
    Notification n = new Notification(android.R.drawable.stat_sys_warning,
            context.getString(R.string.update_title), 0);
    n.flags = Notification.FLAG_AUTO_CANCEL;
    PendingIntent pi = PendingIntent.getActivity(context, 0,
            new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + pkg)),
            PendingIntent.FLAG_UPDATE_CURRENT);
    n.setLatestEventInfo(context, context.getString(R.string.update_title),
            context.getString(R.string.update_message), pi);

    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(0, n);
}

From source file:com.tct.email.NotificationController.java

/**
 * Show (or update) a send failed notification. If tapped, the user is taken to the OUTBOX view
 * where he can view the list of failed mails. if tap retry,mail will retry to send.
 *//*from  w ww . j  a v a 2s. c om*/
public void showMailSendFaildNotification(long accountId, int number) {
    Account account = Account.restoreAccountWithId(mContext, accountId);
    if (account == null) {
        LogUtils.e(LOG_TAG, "Null account during notification SEND_FAILED ");
        return;
    }
    NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext);
    ContentResolver conentResolver = mContext.getContentResolver();
    Folder UIFolder = queryUIFolder(conentResolver, accountId);
    Folder UIInboxFolder = queryUIInboxFolder(conentResolver, accountId);
    com.tct.mail.providers.Account UIAccount = queryUIaccount(conentResolver, accountId);
    if (UIFolder == null || UIAccount == null || UIInboxFolder == null) {
        LogUtils.e(LOG_TAG,
                "Null UIFolder or null UIAccount during showMailSendFaildNotification,do nothing,just return ");
        return;
    }
    //get account address
    String accountAddress = account.getEmailAddress();
    //get account senderName,if null,use address instand
    String senderName = account.getSenderName();
    //get the notification's title(2 emails not send or Email not send)
    String title = createTitle(number);
    //get the warning icon
    Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_warning_grey);

    //get the conversation
    final Uri.Builder uriBuilder = UIFolder.conversationListUri.buildUpon();
    Cursor conversationsCursor = mContext.getContentResolver().query(uriBuilder.build(),
            UIProvider.CONVERSATION_PROJECTION, null, null, null);

    //The Martro style only supported after android L.
    //TS:kaifeng.lu 2016-01-04 EMAIL BUGFIX_1272060   MOD_S
    if (com.tct.mail.utils.Utils.isRunningLOrLater()) {
        notification.setColor(mContext.getResources().getColor(R.color.notification_icon_mail_orange));
        //TS:kaifeng.lu 2016-01-04 EMAIL BUGFIX_1272060   MOD_E
        NotificationCompat.InboxStyle digest = new NotificationCompat.InboxStyle(notification);

        // query current account's outbox mails.and get the subject.
        // Subject1;
        // Subject2;
        // Subject3;
        if (conversationsCursor != null) {
            try {
                while (conversationsCursor.moveToNext()) {
                    final Conversation conversation = new Conversation(conversationsCursor);
                    String sub = createSubject(conversation);
                    digest.addLine(sub);
                }
            } catch (Exception e) {
                LogUtils.e(LOG_TAG, "exception happen during get notification subject", e.getMessage());
            } finally {
                if (conversationsCursor != null) {
                    conversationsCursor.close();
                }
            }
        }

        //only 1 mails failed, show the subject and its senderName and emailAddress.
        //Line1: show the mail's subject ,same with METHOD: notification.setContentText
        //TS:kaifeng.lu 2015-11-20 EMAIL BUGFIX_709873  MOD_S
        if (number > 0) {
            if (TextUtils.isEmpty(senderName)) {
                //TS:kaifeng.lu 2016-01-04 EMAIL BUGFIX_1272060   DEL
                //                    digest.addLine(accountAddress);
            } else {
                digest.addLine(senderName); // Line2: show the senderName,same with METHOD:Notification#setSubText()
            }
            digest.setSummaryText(accountAddress);
        }
        //TS:kaifeng.lu 2015-11-20 EMAIL BUGFIX_709873  MOD_E
        notification.setContentTitle(title);
        notification.setTicker(accountAddress);
        notification.setLargeIcon(icon);
        notification.setSmallIcon(R.drawable.ic_notification_mail_24dp);
        //set the content click/tap intent
        //it will trigger going to OUBOX
        Intent toOutboxAction = Utils.createViewFolderIntent(mContext, UIFolder.folderUri.fullUri, UIAccount);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, toOutboxAction,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setContentIntent(contentIntent);
        //set the action click intent.
        //it will trigger the refresh.
        PendingIntent actionIntent = createRefreshIntent(UIFolder, true, account.mId);
        notification.addAction(R.drawable.ic_refresh_grey_24dp,
                mContext.getResources().getString(R.string.retry), actionIntent);

        //set the failed mails.
        //TS:kaifeng.lu 2016-01-04 EMAIL BUGFIX_1272060   DEL
        //            notification.setNumber(number);

        //set the light and sound
        FolderPreferences folderPreferences = new FolderPreferences(mContext, UIAccount.getAccountId(),
                UIInboxFolder, true);
        boolean vibrate = folderPreferences.isNotificationVibrateEnabled();
        //not give sound for retry notification
        //String ringtoneUri = folderPreferences.getNotificationRingtoneUri();
        int defaults = 0;
        if (vibrate) {
            defaults |= Notification.DEFAULT_VIBRATE;
        }
        // TS: zheng.zou 2015-09-10 EMAIL BUGFIX-557052 MOD_S
        //defaults |= Notification.FLAG_SHOW_LIGHTS;
        defaults |= Notification.DEFAULT_LIGHTS;
        notification.setDefaults(defaults);
        notification.setLights(0xff00ff00, 280, 2080);
        // TS: zheng.zou 2015-09-10 EMAIL BUGFIX-557052 MOD_E
        // notification.setSound(TextUtils.isEmpty(ringtoneUri) ? null
        //          : Uri.parse(ringtoneUri));
        //  LogUtils.i(LOG_TAG, "failed email in %s vibrateWhen: %s, playing notification: %s",
        //          LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()), vibrate);
        // TS: chao.zhang 2015-09-24 EMAIL FEATURE-585337 MOD_S
        //NOTE: UE:the notification only can be cancel by click.
        notification.setOngoing(true);
    }
    Notification warnningNotification = notification.build();
    warnningNotification.flags |= Notification.FLAG_AUTO_CANCEL;
    //TS:kaifeng.lu 2015-11-20 EMAIL BUGFIX_709873  MOD_S
    mNotificationManager.notify(getSendFailedNotificationId(accountId), warnningNotification);
    //TS:kaifeng.lu 2015-11-20 EMAIL BUGFIX_709873  MOD_E
    // TS: chao.zhang 2015-09-24 EMAIL FEATURE-585337 MOD_S
}