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.racoon.ampdroid.Mp3PlayerService.java

private void setNotifiction() {
    RemoteViews notificationView = new RemoteViews(this.getPackageName(), R.layout.player_notification);
    notificationView.setTextViewText(R.id.notificationSongArtist, getArtist());
    notificationView.setTextViewText(R.id.notificationSongTitle, getCurrentTitle());
    /* 1. Setup Notification Builder */
    Notification.Builder builder = new Notification.Builder(this);

    /* 2. Configure Notification Alarm */
    builder.setSmallIcon(R.drawable.ic_stat_notify).setAutoCancel(true).setWhen(System.currentTimeMillis())
            .setTicker(getCurrentTitle());

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent notifIntent = PendingIntent.getActivity(this, 0, intent, 0);

    builder.setContentIntent(notifIntent);
    builder.setContent(notificationView);

    /* 4. Create Notification and use Manager to launch it */
    Notification notification = builder.getNotification();
    String ns = Context.NOTIFICATION_SERVICE;
    notifManager = (NotificationManager) getSystemService(ns);
    notifManager.notify(NOTIFICATION_ID, notification);
    startForeground(NOTIFICATION_ID, notification);
}

From source file:com.lithiumli.fiction.PlaybackService.java

private void showNotification() {
    Intent launchPlaybackIntent = new Intent(getApplicationContext(), NowPlayingActivity.class);
    launchPlaybackIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, launchPlaybackIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    String title = "(unknown title)";
    String artist = "(unknown artist)";
    String album = "(unknown album)";
    Uri albumArt = Song.DEFAULT_ALBUM;/*from w  w w  .ja v  a  2  s .c  o  m*/
    if (mQueue.getCount() != 0) {
        Song song = mQueue.getCurrent();
        title = song.getTitle();
        artist = song.getArtist();
        album = song.getAlbum();
        albumArt = song.getAlbumArt();

        // TODO see ImageView.resolveUri for a working method
        // if (albumArt.getPath() == null) {
        //     albumArt = Song.DEFAULT_ALBUM;
        // }
    }

    Notification.Builder builder = new Notification.Builder(getApplicationContext());
    builder.setSmallIcon(R.drawable.ic_menu_play).setContentTitle("Playing" + title).setOngoing(true)
            .setContentIntent(pi);
    Notification notification = builder.build();

    RemoteViews customView = new RemoteViews(getPackageName(), R.layout.notification);
    customView.setImageViewUri(R.id.notification_cover, albumArt);
    customView.setTextViewText(R.id.notification_title, title);
    customView.setTextViewText(R.id.notification_subtitle, artist);
    notification.contentView = customView;

    customView = new RemoteViews(getPackageName(), R.layout.notification_big);
    customView.setImageViewUri(R.id.notification_cover, albumArt);
    customView.setImageViewResource(R.id.notification_play_pause,
            mPaused ? R.drawable.ic_menu_play : R.drawable.ic_menu_pause);
    customView.setTextViewText(R.id.notification_title, title);
    customView.setTextViewText(R.id.notification_album, album);
    customView.setTextViewText(R.id.notification_artist, artist);
    customView.setOnClickPendingIntent(R.id.notification_previous, createAction(ACTION_PREV));
    customView.setOnClickPendingIntent(R.id.notification_play_pause, createAction(ACTION_PLAY_PAUSE));
    customView.setOnClickPendingIntent(R.id.notification_next, createAction(ACTION_NEXT));
    notification.bigContentView = customView;

    startForeground(NOTIFICATION_PLAYING, notification);
    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_PLAYING,
            notification);
}

From source file:com.example.android.lnotifications.VisibilityMetadataFragment.java

/**
 * Creates a new notification with a different visibility level.
 *
 * @param visibility The visibility of the notification to be created.
 *
 * @return A Notification instance.//from w  w  w .j  a  v a  2 s.c  o  m
 */
private Notification createNotification(NotificationVisibility visibility) {
    Notification.Builder notificationBuilder = new Notification.Builder(getActivity())
            .setContentTitle("Notification for Visibility metadata");

    notificationBuilder.setVisibility(visibility.getVisibility());
    notificationBuilder.setContentText(String.format("Visibility : %s", visibility.getDescription()));
    notificationBuilder.setSmallIcon(visibility.getNotificationIconId());

    return notificationBuilder.build();
}

From source file:ch.bfh.instacircle.service.NetworkService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    // Initializing the dbHelper in order to get access to the database
    dbHelper = NetworkDbHelper.getInstance(this);

    // Create a pending intent which will be invoked after tapping on the
    // Android notification
    Intent notificationIntent = new Intent(this, NetworkActiveActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent,
            notificationIntent.getFlags());

    // Setting up the notification which is being displayed
    Notification.Builder notificationBuilder = new Notification.Builder(this);
    notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
    notificationBuilder.setContentText("An InstaCircle Chat session is running. Tap to bring in front.");
    notificationBuilder.setSmallIcon(R.drawable.glyphicons_244_conversation);
    notificationBuilder.setContentIntent(pendingNotificationIntent);
    notificationBuilder.setOngoing(true);
    Notification notification = notificationBuilder.getNotification();

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

    notificationManager.notify(TAG, 1, notification);

    udpBroadcastReceiverThreads = new UDPBroadcastReceiverThread[100];
    tcpUnicastReceiverThreads = new TCPUnicastReceiverThread[100];

    // starting 100 threads allocating 100 Ports
    for (int i = 0; i < 50; i++) {
        udpBroadcastReceiverThreads[i] = new UDPBroadcastReceiverThread(this, i + 12300);
        tcpUnicastReceiverThreads[i] = new TCPUnicastReceiverThread(this, i + 12300);

        udpBroadcastReceiverThreads[i].start();
        tcpUnicastReceiverThreads[i].start();
    }//  w  w  w.ja va2  s. c om

    // Register a broadcastreceiver in order to get notification from the UI
    // when a message should be sent
    LocalBroadcastManager.getInstance(this).registerReceiver(messageSendReceiver,
            new IntentFilter("messageSend"));

    // Opening a conversation
    dbHelper.openConversation(getSharedPreferences(PREFS_NAME, 0).getString("password", "N/A"));

    // joining the conversation using the identification in the preferences
    // file
    joinNetwork(getSharedPreferences(PREFS_NAME, 0).getString("identification", "N/A"));

    // start the NetworkActiveActivity
    intent = new Intent(this, NetworkActiveActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
    startActivity(intent);
    return super.onStartCommand(intent, flags, startId);
}

From source file:com.mishiranu.dashchan.content.service.AudioPlayerService.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void refreshPlaybackNotification(boolean recreate) {
    Notification.Builder builder = this.builder;
    if (builder == null || recreate) {
        builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.ic_audiotrack_white_24dp);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(this, AudioPlayerActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);
        TypedArray typedArray = context.obtainStyledAttributes(ICON_ATTRS);
        PendingIntent toggleIntent = PendingIntent.getService(context, 0, obtainIntent(this, ACTION_TOGGLE),
                PendingIntent.FLAG_UPDATE_CURRENT);
        boolean playing = mediaPlayer.isPlaying();
        ViewUtils.addNotificationAction(builder, context, typedArray, playing ? 3 : 2,
                playing ? R.string.action_pause : R.string.action_play, toggleIntent);
        PendingIntent cancelIntent = PendingIntent.getService(context, 0, obtainIntent(this, ACTION_CANCEL),
                PendingIntent.FLAG_UPDATE_CURRENT);
        ViewUtils.addNotificationAction(builder, context, typedArray, 1, R.string.action_stop, cancelIntent);
        typedArray.recycle();//  w  ww.j  av  a2s  .com
        if (C.API_LOLLIPOP) {
            builder.setColor(notificationColor);
        }
        this.builder = builder;
        builder.setContentTitle(getString(R.string.message_file_playback));
        builder.setContentText(getString(R.string.message_download_name_format, fileName));
    }
    startForeground(C.NOTIFICATION_ID_AUDIO_PLAYER, builder.build());
}

From source file:com.mishiranu.dashchan.content.service.AudioPlayerService.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void refreshDownloadingNotification(boolean recreate, boolean error, Uri uri) {
    Notification.Builder builder = this.builder;
    if (builder == null || recreate) {
        builder = new Notification.Builder(this);
        builder.setSmallIcon(
                error ? android.R.drawable.stat_sys_download_done : android.R.drawable.stat_sys_download);
        builder.setDeleteIntent(PendingIntent.getService(context, 0, obtainIntent(this, ACTION_CANCEL),
                PendingIntent.FLAG_UPDATE_CURRENT));
        TypedArray typedArray = context.obtainStyledAttributes(ICON_ATTRS);
        if (error) {
            PendingIntent retryIntent = PendingIntent.getService(
                    context, 0, obtainIntent(this, ACTION_START).setData(uri)
                            .putExtra(EXTRA_CHAN_NAME, chanName).putExtra(EXTRA_FILE_NAME, fileName),
                    PendingIntent.FLAG_UPDATE_CURRENT);
            ViewUtils.addNotificationAction(builder, context, typedArray, 0, R.string.action_retry,
                    retryIntent);/*  w ww.j  a  va2s.co m*/
        } else {
            PendingIntent cancelIntent = PendingIntent.getService(context, 0, obtainIntent(this, ACTION_CANCEL),
                    PendingIntent.FLAG_UPDATE_CURRENT);
            ViewUtils.addNotificationAction(builder, context, typedArray, 1, android.R.string.cancel,
                    cancelIntent);
        }
        typedArray.recycle();
        if (C.API_LOLLIPOP) {
            builder.setColor(notificationColor);
        }
        this.builder = builder;
    }
    if (error) {
        builder.setContentTitle(getString(R.string.message_download_completed));
        builder.setContentText(getString(R.string.message_download_result_format, 0, 1));
        notificationManager.notify(C.NOTIFICATION_ID_AUDIO_PLAYER, builder.build());
    } else {
        builder.setContentTitle(getString(R.string.message_download_audio));
        builder.setContentText(getString(R.string.message_download_name_format, fileName));
        builder.setProgress(progressMax, progress, progressMax == 0 || progress > progressMax || progress < 0);
        startForeground(C.NOTIFICATION_ID_AUDIO_PLAYER, builder.build());
    }
}

From source file:com.mishiranu.dashchan.content.service.PostingService.java

@Override
public void onSendPostSuccess(String key, ChanPerformer.SendPostData data, String chanName, String threadNumber,
        String postNumber) {/*from w w  w . j a  v  a 2s .  com*/
    if (removeTask(key)) {
        String targetThreadNumber = data.threadNumber != null ? data.threadNumber
                : StringUtils.nullIfEmpty(threadNumber);
        if (targetThreadNumber != null && Preferences.isFavoriteOnReply()) {
            FavoritesStorage.getInstance().add(chanName, data.boardName, targetThreadNumber, null, 0);
        }
        StatisticsStorage.getInstance().incrementPostsSent(chanName, data.threadNumber == null);
        DraftsStorage draftsStorage = DraftsStorage.getInstance();
        draftsStorage.removeCaptchaDraft();
        draftsStorage.removePostDraft(chanName, data.boardName, data.threadNumber);
        if (targetThreadNumber != null) {
            String password = Preferences.getPassword(chanName);
            if (StringUtils.equals(password, data.password)) {
                password = null;
            }
            draftsStorage
                    .store(new DraftsStorage.PostDraft(chanName, data.boardName, targetThreadNumber, data.name,
                            data.email, password, data.optionSage, data.optionOriginalPoster, data.userIcon));
        }
        if (targetThreadNumber != null) {
            postNumber = StringUtils.nullIfEmpty(postNumber);
            String comment = data.comment;
            if (comment != null) {
                CommentEditor commentEditor = ChanMarkup.get(chanName).safe()
                        .obtainCommentEditor(data.boardName);
                if (commentEditor != null) {
                    comment = commentEditor.removeTags(comment);
                }
            }
            NewPostData newPostData = new NewPostData(chanName, data.boardName, targetThreadNumber, postNumber,
                    comment, data.threadNumber == null);
            String arrayKey = makeKey(chanName, data.boardName, targetThreadNumber);
            ArrayList<NewPostData> newPostDatas = NEW_POST_DATAS.get(arrayKey);
            if (newPostDatas == null) {
                newPostDatas = new ArrayList<>(1);
                NEW_POST_DATAS.put(arrayKey, newPostDatas);
            }
            newPostDatas.add(newPostData);
            if (newPostData.newThread) {
                PostingService.newThreadData = newPostData;
                PostingService.newThreadDataKey = makeKey(chanName, data.boardName, null);
            }
            Notification.Builder builder = new Notification.Builder(this);
            builder.setSmallIcon(android.R.drawable.stat_sys_upload_done);
            if (C.API_LOLLIPOP) {
                setNotificationColor(builder);
                builder.setPriority(Notification.PRIORITY_HIGH);
                builder.setVibrate(new long[0]);
            } else {
                builder.setTicker(getString(R.string.text_post_sent));
            }
            builder.setContentTitle(getString(R.string.text_post_sent));
            builder.setContentText(
                    buildNotificationText(chanName, data.boardName, targetThreadNumber, postNumber));
            String tag = newPostData.getNotificationTag();
            Intent intent = NavigationUtils.obtainPostsIntent(this, chanName, data.boardName,
                    targetThreadNumber, postNumber, null, 0);
            builder.setContentIntent(
                    PendingIntent.getActivity(this, tag.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT));
            notificationManager.notify(tag, 0, builder.build());
        }
        ArrayList<Callback> callbacks = this.callbacks.get(key);
        if (callbacks != null) {
            for (Callback callback : callbacks) {
                callback.onSendPostSuccess();
            }
        }
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(C.ACTION_POST_SENT));
    }
}

From source file:com.granita.contacticloudsync.syncadapter.AccountSettings.java

@TargetApi(21)
protected void showNotification(int id, String title, String message) {
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder n = new Notification.Builder(context);
    if (Build.VERSION.SDK_INT >= 16) {
        n.setPriority(Notification.PRIORITY_HIGH);
        n.setStyle(new Notification.BigTextStyle().bigText(message));
    }//from   www . j  a va  2 s.  c  o  m
    if (Build.VERSION.SDK_INT >= 20)
        n.setLocalOnly(true);
    if (Build.VERSION.SDK_INT >= 21)
        n.setCategory(Notification.CATEGORY_SYSTEM);
    n.setSmallIcon(R.drawable.ic_launcher);
    n.setContentTitle(title);
    n.setContentText(message);
    nm.notify(id, Build.VERSION.SDK_INT >= 16 ? n.build() : n.getNotification());
}

From source file:com.swetha.easypark.GetIndividualParkingSpotDetails.java

private Notification getNotification(String content) {
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentTitle("EasyPark Notification");
    builder.setContentText(content);//from   ww  w .  j  ava 2s . co m
    builder.setSmallIcon(R.drawable.ic_car);
    builder.setPriority(BIND_IMPORTANT);
    builder.setSound(soundUri);

    // return builder.build();

    return new Notification.BigTextStyle(builder).bigText(content).build();
}