Example usage for android.app Notification.Builder setSmallIcon

List of usage examples for android.app Notification.Builder setSmallIcon

Introduction

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

Prototype

@UnsupportedAppUsage
public void setSmallIcon(Icon icon) 

Source Link

Document

Used when notifying to clean up legacy small icons.

Usage

From source file:com.busan.cw.clsp20120924.gcm.CommonUtilities.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//* w  w w .  ja  va  2s.c o m*/
@SuppressWarnings("deprecation")
public static void generateNotification(Context context, GCMMessageData data) {
    String message = data.message;
    String url = data.url;
    int lightColor = 0xffff0000;
    long[] vibrate = new long[] { 0, 200, 300, 200 };

    NotificationManager mNotiManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent;
    if (data.url == null || "".equals(data.url))
        contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    else
        contentIntent = PendingIntent.getActivity(context, 0, new Intent(Intent.ACTION_VIEW, Uri.parse(url)),
                0);

    if (Build.VERSION.SDK_INT >= 14) {
        Notification.Builder mBuilder = new Notification.Builder(context);
        mBuilder.setVibrate(vibrate);
        mBuilder.setLights(lightColor, 500, 500);
        mBuilder.setContentTitle(data.title);
        mBuilder.setSmallIcon(com.busan.cw.clsp20120924.R.drawable.ic_launcher);
        mBuilder.setAutoCancel(true);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setContentText(message);
        mBuilder.setTicker(message);
        if (Build.VERSION.SDK_INT >= 16)
            mNotiManager.notify(data.action, mBuilder.build());
        else
            mNotiManager.notify(data.action, mBuilder.getNotification());
    } else {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setVibrate(vibrate);
        mBuilder.setLights(lightColor, 500, 500);
        mBuilder.setContentTitle(data.title);
        mBuilder.setSmallIcon(com.busan.cw.clsp20120924.R.drawable.ic_launcher);
        mBuilder.setAutoCancel(true);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setContentText(data.message);
        mBuilder.setTicker(data.message);
        mNotiManager.notify(data.action, mBuilder.getNotification());
    }

}

From source file:org.ttrssreader.utils.Utils.java

@SuppressWarnings("deprecation")
public static Notification buildNotification(Context context, int icon, CharSequence ticker, CharSequence title,
        CharSequence text, boolean autoCancel, Intent intent) {
    Notification notification = null;
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    try {/*from   w  w  w  .  j ava 2s.  c  o  m*/
        Notification.Builder builder = new Notification.Builder(context);
        builder.setSmallIcon(icon);
        builder.setTicker(ticker);
        builder.setWhen(System.currentTimeMillis());
        builder.setContentTitle(title);
        builder.setContentText(text);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(autoCancel);
        if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN)
            notification = builder.getNotification();
        else
            notification = builder.build();
    } catch (Exception re) {
        Log.e(TAG, "Exception while building notification. Does your device propagate the right API-Level? ("
                + Build.VERSION.SDK_INT + ")", re);
    }

    return notification;
}

From source file:com.matze5800.paupdater.Functions.java

public static void Notify(Context context, String text) {
    Log.i("notify", text);
    Notification.Builder nBuilder;
    nBuilder = new Notification.Builder(context);
    Intent resultIntent = new Intent(context, Cancel.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(Cancel.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    nBuilder.setContentIntent(resultPendingIntent);
    nBuilder.setSmallIcon(R.drawable.ic_launcher);
    nBuilder.setContentTitle("PA Updater");
    nBuilder.setContentText(text);//from   w  w  w .ja  v  a  2s. co m
    nBuilder.setAutoCancel(false);
    nBuilder.setOngoing(true);
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, nBuilder.build());
}

From source file:com.neal.repairer.ease.receiver.CallReceiver.java

@Override
public void onReceive(Context context, Intent intent) {

    //username//from  ww w .j a v a2s  . co  m
    String from = intent.getStringExtra("from");

    EMLog.d("CallReceiver", "app received a incoming call");
    //?????
    if (isRunningForeground(context)) {
        context.startActivity(new Intent(context, VideoCallActivity.class).putExtra("username", from)
                .putExtra("isComingCall", true).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    } else {
        // PendingIntentIntent???????activityPendingIntent,?Notification?Activity
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, VideoCallActivity.class).putExtra("username", from)
                        .putExtra("isComingCall", true).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
                0);
        //
        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher_round);
        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
        builder.setAutoCancel(true);
        builder.setTicker("???");
        builder.setContentText("?");
        builder.setContentTitle("?");
        builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
        //??
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }
}

From source file:com.github.chenxiaolong.dualbootpatcher.patcher.PatcherService.java

private void patchFile(Bundle data) {
    Intent resultIntent = new Intent(this, MainActivity.class);
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    resultIntent.setAction(Intent.ACTION_MAIN);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);

    final Notification.Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setOngoing(true);//from  www. ja  v a  2  s .co  m
    builder.setContentTitle(getString(R.string.overall_progress));
    builder.setContentIntent(pendingIntent);
    builder.setProgress(0, 0, true);

    final NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(1, builder.build());

    Patcher.ProgressListener listener = new Patcher.ProgressListener() {
        @Override
        public void onProgressUpdated(long bytes, long maxBytes) {
            updateProgress(bytes, maxBytes);
        }

        @Override
        public void onFilesUpdated(long files, long maxFiles) {
            builder.setContentText(String.format(getString(R.string.overall_progress_files), files, maxFiles));
            builder.setProgress((int) maxFiles, (int) files, false);
            nm.notify(1, builder.build());

            updateFiles(files, maxFiles);
        }

        @Override
        public void onDetailsUpdated(String text) {
            updateDetails(text);
        }
    };

    Bundle result = PatcherUtils.patchFile(this, data, listener);

    if (result != null) {
        String newFile = result.getString(PatcherUtils.RESULT_PATCH_FILE_NEW_FILE);
        int errorCode = result.getInt(PatcherUtils.RESULT_PATCH_FILE_ERROR_CODE);
        boolean failed = result.getBoolean(PatcherUtils.RESULT_PATCH_FILE_FAILED);
        onPatchedFile(failed, errorCode, newFile);
    }

    nm.cancel(1);
}

From source file:com.dimasdanz.kendalipintu.util.CommonUtilities.java

public static void generateNotification(Context context, String message, String time, String info) {
    try {/*  w  ww  .  j  a  v  a  2 s.  com*/
        NotificationManager manager;
        int notificationID = 73;

        Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_stat_notification);

        Notification.Builder builder = new Notification.Builder(context);
        Intent resultIntent = new Intent(context, LogActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

        stackBuilder.addParentStack(LogActivity.class);
        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        Spannable sb = new SpannableString(message + " " + time + "-" + info);
        sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, message.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        builder.setAutoCancel(true);
        builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);
        builder.setContentTitle(context.getString(R.string.notification_title));
        builder.setContentText(sb);
        builder.setTicker(context.getString(R.string.notification_ticker));
        builder.setNumber(++msgCounter);
        builder.setSmallIcon(R.drawable.ic_stat_notification);
        builder.setLargeIcon(largeIcon);
        builder.setContentIntent(resultPendingIntent);

        if (msgCounter > 1) {
            Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
            if (msgCounter > 6) {
                name[0] = new SpannableString("...");
                name[1] = name[2];
                name[2] = name[3];
                name[3] = name[4];
                name[4] = name[5];
                name[5] = sb;
            } else {
                name[msgCounter - 1] = sb;
            }

            inboxStyle.setBigContentTitle(context.getString(R.string.notification_title));
            inboxStyle.setSummaryText(
                    msgCounter + " " + context.getString(R.string.notification_title) + " Baru");

            for (int i = name.length; i > 0; i--) {
                inboxStyle.addLine(name[i - 1]);
            }

            builder.setStyle(inboxStyle);
            builder.setContentText(msgCounter + " " + context.getString(R.string.notification_title) + " Baru");
        } else {
            name[0] = sb;
        }
        manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(notificationID, builder.build());
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

}

From source file:com.android.shell.BugreportReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT);
    final File screenshotFile = getFileExtra(intent, EXTRA_SCREENSHOT);

    // Files are kept on private storage, so turn into Uris that we can
    // grant temporary permissions for.
    final Uri bugreportUri = FileProvider.getUriForFile(context, AUTHORITY, bugreportFile);
    final Uri screenshotUri = FileProvider.getUriForFile(context, AUTHORITY, screenshotFile);

    Intent sendIntent = buildSendIntent(context, bugreportUri, screenshotUri);
    Intent notifIntent;/*from w  w w .  j a v a 2  s  .  co  m*/

    // Send through warning dialog by default
    if (getWarningState(context, STATE_SHOW) == STATE_SHOW) {
        notifIntent = buildWarningIntent(context, sendIntent);
    } else {
        notifIntent = sendIntent;
    }
    notifIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    final Notification.Builder builder = new Notification.Builder(context);
    builder.setSmallIcon(com.android.internal.R.drawable.stat_sys_adb);
    builder.setContentTitle(context.getString(R.string.bugreport_finished_title));
    builder.setTicker(context.getString(R.string.bugreport_finished_title));
    builder.setContentText(context.getString(R.string.bugreport_finished_text));
    builder.setContentIntent(
            PendingIntent.getActivity(context, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT));
    builder.setAutoCancel(true);
    NotificationManager.from(context).notify(TAG, 0, builder.build());

    // Clean up older bugreports in background
    final PendingResult result = goAsync();
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            FileUtils.deleteOlderFiles(bugreportFile.getParentFile(), MIN_KEEP_COUNT, MIN_KEEP_AGE);
            result.finish();
            return null;
        }
    }.execute();
}

From source file:com.better.alarm.presenter.alert.AlarmAlertReceiver.java

private void onSoundExpired(int id) {
    Intent dismissAlarm = new Intent(mContext, AlarmAlertReceiver.class);
    dismissAlarm.setAction(ACTION_CANCEL_NOTIFICATION);
    dismissAlarm.putExtra(Intents.EXTRA_ID, id);
    PendingIntent intent = PendingIntent.getBroadcast(mContext, id, dismissAlarm, 0);
    // Update the notification to indicate that the alert has been
    // silenced.//  www.  j a v  a 2s  . c o  m
    String label = alarm.getLabelOrDefault(mContext);
    int autoSilenceMinutes = Integer
            .parseInt(PreferenceManager.getDefaultSharedPreferences(mContext).getString("auto_silence", "10"));
    String text = mContext.getString(R.string.alarm_alert_alert_silenced, autoSilenceMinutes);

    Notification.Builder nb = new Notification.Builder(mContext);
    nb.setAutoCancel(true);
    nb.setSmallIcon(R.drawable.stat_notify_alarm);
    nb.setWhen(Calendar.getInstance().getTimeInMillis());
    nb.setContentIntent(intent);
    nb.setContentTitle(label);
    nb.setContentText(text);
    nb.setTicker(text);
    Notification n = nb.getNotification();

    // We have to cancel the original notification since it is in the
    // ongoing section and we want the "killed" notification to be a plain
    // notification.
    nm.cancel(id);
    nm.notify(id, n);
}

From source file:com.ucmap.dingdinghelper.services.TimingService.java

private void increasePriority() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        Notification.Builder mBuilder = new Notification.Builder(this);
        mBuilder.setSmallIcon(R.mipmap.app_logo);
        startForeground(NOTIFICATION_ID, mBuilder.build());
        InnerService.startInnerService(this);
    } else {//w  ww. j a v a2s .  c o  m
        startForeground(NOTIFICATION_ID, new Notification());
    }
}

From source file:com.hybris.mobile.lib.location.geofencing.service.GeofencingIntentService.java

/**
 * Send a notification when a geofence is triggered
 *
 * @param geofence           the geofence triggered
 * @param notification       the notification object
 * @param geofenceTransition the geofence transition type
 *//*from ww w  .  j  ava 2  s.co m*/
protected void sendNotification(Geofence geofence, GeofenceObject.Notification notification,
        int geofenceTransition) {

    if (notification != null) {

        // Notification
        String notificationContentTitle = notification.getNotificationTitle();
        String notificationContentText = notification.getNotificationText();
        int notificationIconResId = notification.getNotificationIconResId();

        Notification.Builder builder = new Notification.Builder(this);

        builder.setContentTitle(notificationContentTitle).setContentText(notificationContentText);

        if (notificationIconResId > 0) {
            builder.setSmallIcon(notificationIconResId);
        }

        try {
            // Intent on click on the notification
            if (StringUtils.isNotBlank(notification.getIntentClassDestination())) {
                Class<?> intentClassDestination = Class.forName(notification.getIntentClassDestination());

                // Create an explicit content Intent that starts the Activity defined in intentClassDestination
                Intent notificationIntent = new Intent(this, intentClassDestination);

                // Geofence Id to pass to the activity in order to retrieve the object
                if (notification.getIntentBundle() != null) {
                    GeofenceObject.IntentBundle intentBundle = notification.getIntentBundle();
                    notificationIntent.putExtra(intentBundle.getKeyName(), intentBundle.getBundle());

                    // Easter egg :)
                    if (intentBundle.getBundle().getBoolean(GeofencingConstants.EXTRA_PLAY_SOUND)) {
                        MediaPlayer mediaPlayer;
                        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
                            Log.d(TAG, "Playing entering geofence sound");
                            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.entering_geofence);
                        } else {
                            Log.d(TAG, "Playing exiting geofence sound");
                            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.leaving_geofence);
                        }

                        mediaPlayer.start();
                    }
                }

                PendingIntent notificationPendingIntent = PendingIntent.getActivity(this,
                        geofence.getRequestId().hashCode(), notificationIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                builder.setContentIntent(notificationPendingIntent);
            }

        } catch (ClassNotFoundException e) {
            Log.e(TAG, "Unable to find class " + notification.getIntentClassDestination() + "."
                    + e.getLocalizedMessage());
        }

        // Constructing the Notification and setting the flag to auto remove the notification when the user click on it
        Notification notificationView;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            notificationView = builder.build();
        } else {
            notificationView = builder.getNotification();
        }

        notificationView.flags = Notification.FLAG_AUTO_CANCEL;
        notificationView.defaults = Notification.DEFAULT_ALL;

        // Get an instance of the Notification manager
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);

        // Issue the notification
        mNotificationManager.notify(UUID.randomUUID().toString().hashCode(), notificationView);
    } else {
        Log.e(TAG, "Notification empty for Geofence " + geofence);
    }

}