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.sdk.download.providers.downloads.DownloadNotification.java

private void updateCompletedNotification(Collection<DownloadInfo> downloads) {
    for (DownloadInfo download : downloads) {
        if (!isCompleteAndVisible(download)) {
            continue;
        }// www .ja v a 2 s .  com
        // Add the notifications
        Notification n = new Notification();
        n.icon = android.R.drawable.stat_sys_download_done;
        n.flags |= Notification.FLAG_AUTO_CANCEL;
        long id = download.mId;
        String title = download.mTitle;
        if (title == null || title.length() == 0) {
            title = mContext.getResources().getString(R.string.zuimeia_sdk_download_download_unknown_title);
        }
        Uri contentUri = ContentUris.withAppendedId(Downloads.getAllDownloadsContentURI(mContext), id);
        String caption;
        Intent intent;
        if (Downloads.isStatusError(download.mStatus)) {
            caption = mContext.getResources()
                    .getString(R.string.zuimeia_sdk_download_notification_download_failed);
            intent = new Intent(Constants.ACTION_LIST);
        } else {
            caption = mContext.getResources()
                    .getString(R.string.zuimeia_sdk_download_notification_download_complete);
            if (download.mDestination == Downloads.DESTINATION_EXTERNAL) {
                intent = new Intent(Constants.ACTION_OPEN);
            } else {
                intent = new Intent(Constants.ACTION_LIST);
            }
        }
        intent.setClassName(mContext.getPackageName(), DownloadReceiver.class.getName());
        intent.setData(contentUri);

        n.when = download.mLastMod;
        n.setLatestEventInfo(mContext, title, caption, PendingIntent.getBroadcast(mContext, 0, intent, 0));

        intent = new Intent(Constants.ACTION_HIDE);
        intent.setClassName(mContext.getPackageName(), DownloadReceiver.class.getName());
        intent.setData(contentUri);
        // n.deleteIntent = PendingIntent.getBroadcast(mContext, 0, intent,
        // 0); //notification??
        mSystemFacade.postNotification(download.mId, n);
    }
}

From source file:com.unfc.choicecustomercare.gcmservices.MyGcmListenerService.java

private void sendNotificationForTakeBreakAccept(String message, String fromId, String toId) {

    boolean loggedOut = CustomPreferences.getPreferences(Constants.PREF_LOGGED_OUT, false);
    Intent intent;/*  w w  w  .  j  a v a2  s.c  om*/
    if (loggedOut) {
        intent = new Intent(this, LoginActivity.class);

    } else {
        intent = new Intent(this, MainActivity.class);
    }

    intent.setAction("takeBreakAccept");
    intent.putExtra("fromId", fromId);
    intent.putExtra("toId", toId);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Intent intentAccept = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingAccept = PendingIntent.getActivity(this, 1, intentAccept, PendingIntent.FLAG_ONE_SHOT);
    //
    Intent intentDecline = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingDecline = PendingIntent.getActivity(this, 2, intentDecline,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(getString(R.string.app_name))
            .setContentText(message).setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent).setAutoCancel(true);

    notificationBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
    notificationBuilder.setAutoCancel(true);

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

    notificationManager.notify(Constants.TAKE_A_BREAK_NOTIFICATION_ID, notificationBuilder.build());

}

From source file:com.microsoft.azure.engagement.fragment.OutOfAppPushNotificationsFragment.java

/**
 * Method that displays an out-of-app notification.
 *
 * @param context           The context/*from w w  w.  j  a v  a 2 s  .c  o m*/
 * @param notificationStyle The notificationStyle
 * @param actionUrl         The actionUrl
 */
private final void displayNotification(Context context, NotificationStyle notificationStyle, String actionUrl) {
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    final Resources resources = context.getResources();
    builder.setSmallIcon(R.drawable.ic_notification_default);
    builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher));

    builder.setContentTitle(context.getString(R.string.out_of_app_push_notification_content_title));
    builder.setContentText(context.getString(R.string.out_of_app_push_notification_content_message));
    final Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
    notificationIntent.putExtra(ReboundActivity.ACTION_URL_EXTRA,
            getString(R.string.deeplink_recent_product_updates));

    notificationIntent.setData(Uri.parse(actionUrl));
    final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    final Style style;

    switch (notificationStyle) {
    case normal:
        style = null;
        builder.setContentTitle(context.getString(R.string.out_of_app_push_notification_content_title));
        builder.setContentText(context.getString(R.string.out_of_app_push_notification_content_message));
        break;
    case bigText:
        final BigTextStyle bigTextStyle = new BigTextStyle();
        bigTextStyle.setBigContentTitle(context.getString(R.string.out_of_app_push_notification_content_title));
        bigTextStyle.setSummaryText(context.getString(R.string.out_of_app_push_notification_content_message));
        bigTextStyle.bigText(context.getString(R.string.out_of_app_push_notification_content_big_text));
        builder.setContentTitle(context.getString(R.string.out_of_app_push_notification_content_title));
        builder.setContentText(context.getString(R.string.out_of_app_push_notification_content_message));
        style = bigTextStyle;
        break;
    case bigImage:
        final BigPictureStyle bigPictureStyle = new BigPictureStyle();
        bigPictureStyle
                .setBigContentTitle(context.getString(R.string.out_of_app_push_notification_content_title));
        bigPictureStyle
                .setSummaryText(context.getString(R.string.out_of_app_push_notification_content_message));
        bigPictureStyle
                .bigPicture(BitmapFactory.decodeResource(resources, R.drawable.ic_notification_big_image));
        bigPictureStyle.bigLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher));
        builder.setContentTitle(context.getString(R.string.out_of_app_push_notification_content_title));
        builder.setContentText(context.getString(R.string.out_of_app_push_notification_content_message));
        style = bigPictureStyle;
        break;
    default:
        style = null;
        break;
    }

    builder.setStyle(style);
    builder.setContentIntent(pendingIntent);

    // Share action
    final Intent sharingIntent = new Intent();
    sharingIntent.setAction(Intent.ACTION_SEND);
    sharingIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.notification_share_message));
    sharingIntent.setType("text/plain");
    final PendingIntent actionShareIntent = PendingIntent.getActivity(getActivity(), 0, sharingIntent, 0);
    builder.addAction(R.drawable.ic_out_of_app_share, getString(R.string.notification_share_button_title),
            actionShareIntent);

    // Feedback action
    final Intent feedbackIntent = new Intent();
    feedbackIntent.setAction(Intent.ACTION_VIEW);
    final Uri data = Uri.parse("mailto:");
    feedbackIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.notification_feedback_email_subject));
    feedbackIntent.setData(data);
    final PendingIntent actionFeedbackIntent = PendingIntent.getActivity(getActivity(), 0, feedbackIntent, 0);
    builder.addAction(R.drawable.ic_out_of_app_send_feedback,
            getString(R.string.notification_feedback_button_title), actionFeedbackIntent);

    final Notification notification = builder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    final NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(OutOfAppPushNotificationsFragment.NOTIFICATION_ID++, notification);

}

From source file:it.mb.whatshare.GCMIntentService.java

/**
 * Creates a notification informing the user that new content is to be
 * shared.//  ww w .j av a 2 s  .co m
 * 
 * @param context
 *            the current application context
 * @param message
 *            the message to be shared
 * @param useWhatsapp
 *            whether the content should be shared bypassing the app choice
 *            dialog calling whatsapp directly instead
 */
@SuppressWarnings("deprecation")
public void generateNotification(Context context, String message, boolean useWhatsapp) {
    // @formatter:off
    String title = context.getString(R.string.whatshare);
    Class<?> dst = useWhatsapp ? SendToWhatsappActivity.class : SendToAppActivity.class;
    // setAction is called so filterEquals() always returns false for all
    // our intents
    Intent whatshareIntent = new Intent(context, dst).putExtra("message", message)
            .setAction(String.valueOf(System.currentTimeMillis())).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent intent = PendingIntent.getActivity(context, 0, whatshareIntent,
            Intent.FLAG_ACTIVITY_NEW_TASK);

    Notification notification = null;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(useWhatsapp ? R.drawable.notification_icon : R.drawable.whatshare_logo_notification,
                    0)
            .setContentTitle(title).setContentText(message).setContentIntent(intent)
            .setDefaults(Notification.DEFAULT_ALL);
    // @formatter:on

    if (Build.VERSION.SDK_INT > 15) {
        notification = buildForJellyBean(builder);
    } else {
        notification = builder.getNotification();
    }
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
            .notify(counter.incrementAndGet(), notification);
}

From source file:net.geniecode.ttr.ScheduleReceiver.java

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void setNotification(Context context) {
    ScrollingText = context.getString(R.string.schedule_postponed_scroll);
    NotificationText = context.getString(R.string.schedule_postponed_notify);

    // Trigger a notification that, when clicked, will activate airplane mode
    mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, ActivateFlightMode.class), PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification;//from w  ww  . j  a  va2s  .  c o  m

    if (android.os.Build.VERSION.SDK_INT >= 11) {
        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentIntent(contentIntent).setSmallIcon(R.drawable.ic_launcher).setTicker(ScrollingText)
                .setWhen(System.currentTimeMillis()).setAutoCancel(true).setOnlyAlertOnce(true)
                .setDefaults(Notification.DEFAULT_LIGHTS).setContentTitle(context.getText(R.string.app_name))
                .setContentText(NotificationText);
        if (android.os.Build.VERSION.SDK_INT >= 16) {
            notification = builder.build();
        } else {
            notification = builder.getNotification();
        }
    } else {
        notification = new Notification(R.drawable.ic_launcher, ScrollingText, System.currentTimeMillis());
        notification.setLatestEventInfo(context, context.getText(R.string.app_name), NotificationText,
                contentIntent);
        notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
    }
    mNotificationManager.notify(NOTIFICATION_ID, notification);
}

From source file:com.airbop.client.GCMIntentService.java

private static void generateImageNotification(Context context, String title, String message, String url,
        String image_url, String large_icon) {

    // The bitmap to download
    Bitmap message_bitmap = null;/*  w w  w .j  ava 2  s  .  c o  m*/
    // Should we download the image?
    if ((image_url != null) && (!image_url.equals(""))) {
        message_bitmap = AirBopImageDownloader.downloadBitmap(image_url);
    }
    // If we didn't get the image, we're out of here
    if (message_bitmap == null) {
        generateNotification(context, title, message, url, large_icon);
        return;
    }

    int icon = R.drawable.ic_stat_gcm;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    if ((title == null) || (title.equals(""))) {
        title = context.getString(R.string.app_name);
    }

    Intent notificationIntent = null;
    if ((url == null) || (url.equals(""))) {
        //just bring up the app
        notificationIntent = new Intent(context, DemoActivity.class);
    } else {
        //Launch the URL
        notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setData(Uri.parse(url));
        notificationIntent.addCategory(Intent.CATEGORY_BROWSABLE);
    }

    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(context).setContentTitle(title)
            .setContentText(message).setContentIntent(intent).setSmallIcon(icon)
            .setLargeIcon(decodeImage(large_icon)).setWhen(when)
            .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(message_bitmap)).build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

From source file:com.online.fullsail.SaveWebMedia.java

@Override
protected void onPostExecute(File download) {
    String fileString = download.toString();
    mNM.cancel(timestamp);//from ww  w . ja  va 2  s .  co m
    System.gc();
    String fileName = fileString.substring(fileString.lastIndexOf('/') + 1, fileString.length());
    String fileExt = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length());
    File downFile = new File(externalData, fileName);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(downFile), "application/" + fileExt);
    List<ResolveInfo> intents = this.context.getPackageManager().queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    if (intents == null || intents.size() == 0) {
        intent.setDataAndType(Uri.fromFile(downFile), "video/" + fileExt);
        intents = this.context.getPackageManager().queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        if (intents == null || intents.size() == 0) {
            intent.setDataAndType(Uri.fromFile(downFile), "image/" + fileExt);
            intents = this.context.getPackageManager().queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
            if (intents == null || intents.size() == 0) {
                intent = new Intent(Intent.ACTION_SEARCH);
                if (Integer.parseInt(Build.VERSION.SDK) > Build.VERSION_CODES.ECLAIR) {
                    intent.setPackage("com.android.vending");
                }
                intent.putExtra("query", fileExt);

            }
        }
    }
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent contentIntent = PendingIntent.getActivity(this.context, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Notification notification = null;
    Resources resources = this.context.getResources();
    String[] mediaTypes = resources.getStringArray(R.array.media);
    boolean acceptedType = false;
    for (int i = 0; i < mediaTypes.length; i++) {
        if (fileExt.toLowerCase().equals(mediaTypes[i])) {
            acceptedType = true;
        }
    }
    if (acceptedType) {
        Bitmap preview = null;
        String[] videoTypes = resources.getStringArray(R.array.movFiles);
        for (int i = 0; i < videoTypes.length; i++) {
            if (fileExt.toLowerCase().equals(videoTypes[i])) {
                preview = getVideoFrame(fileString);
            }
        }
        String[] imageTypes = resources.getStringArray(R.array.movFiles);
        for (int i = 0; i < imageTypes.length; i++) {
            if (fileExt.toLowerCase().equals(imageTypes[i])) {
                preview = decodeBitmapFile(fileString);
            }
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this.context);
        builder.setTicker(this.context.getString(R.string.notify_download_complete))
                .setWhen(System.currentTimeMillis())
                .setContentTitle(this.context.getString(R.string.Document_complete))
                .setContentText(this.context.getString(R.string.Pdf_completed, fileName))
                .setSmallIcon(R.drawable.icon).setAutoCancel(true).setPriority(Notification.PRIORITY_HIGH)
                .setContentIntent(contentIntent);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            notification = new NotificationCompat.BigPictureStyle(builder).bigPicture(preview).build();
        } else {
            notification = builder.build();
        }
    } else {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this.context);
        builder.setContentTitle(this.context.getString(R.string.Document_complete))
                .setContentText(this.context.getString(R.string.Pdf_completed, fileName))
                .setSmallIcon(R.drawable.icon).setAutoCancel(true).setPriority(Notification.PRIORITY_HIGH)
                .setContentIntent(contentIntent);
        notification = builder.build();
    }
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    mNM.notify(timestamp, notification);
}

From source file:com.gelakinetic.mtgfam.helpers.DbUpdaterService.java

protected void showUpdatedNotification(List<String> newStuff) {
    if (newStuff.size() < 1) {
        return;/*from   w w w.ja v a 2s .  co  m*/
    }

    String title = getString(R.string.app_name);
    String body = getString(R.string.update_added) + " ";
    for (int i = 0; i < newStuff.size(); i++) {
        body += newStuff.get(i);
        if (i < newStuff.size() - 1) {
            body += ", ";
        }
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this.getApplicationContext());
    Notification notification = builder.setContentTitle(title).setContentText(body)
            .setSmallIcon(R.drawable.rt_notification_icon).setContentIntent(mNotificationIntent)
            .setWhen(System.currentTimeMillis()).build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(UPDATED_NOTIFICATION, notification);
}

From source file:com.airbop.library.simple.AirBopGCMIntentService.java

private static void generateNotification(Context context, String title, String message, String url,
        String large_icon) {//from w w  w  .  ja v a 2 s  . com

    //int icon = R.drawable.ic_stat_gcm;
    AirBopManifestSettings airBop_settings = CommonUtilities.loadDataFromManifest(context);

    int icon = airBop_settings.mNotificationIcon;

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    //if ((title == null) || (title.equals(""))) {
    if (title == null) {
        title = airBop_settings.mDefaultNotificationTitle;
    }

    Intent notificationIntent = null;
    if ((url == null) || (url.equals(""))) {
        //just bring up the app
        if (context != null) {
            ClassLoader class_loader = context.getClassLoader();
            if (class_loader != null) {
                try {
                    if (airBop_settings.mDefaultNotificationClass != null) {
                        notificationIntent = new Intent(context,
                                Class.forName(airBop_settings.mDefaultNotificationClass));
                    }
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    //notificationIntent = new Intent(Intent.ACTION_VIEW);
                }
            }
        }

    } else {
        //Launch the URL
        notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setData(Uri.parse(url));
        notificationIntent.addCategory(Intent.CATEGORY_BROWSABLE);
    }
    PendingIntent intent = null;
    // set intent so it does not start a new activity
    if (notificationIntent != null) {
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    }

    Builder notificationBuilder = new NotificationCompat.Builder(context).setContentTitle(title)
            .setContentText(message).setLargeIcon(decodeImage(large_icon)).setWhen(when)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message));
    if (intent != null) {
        notificationBuilder.setContentIntent(intent);
    }
    if (icon != 0) {
        notificationBuilder.setSmallIcon(icon);
    }
    Notification notification = notificationBuilder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);

}