Example usage for android.support.v4.app NotificationCompat.Builder NotificationCompat.Builder

List of usage examples for android.support.v4.app NotificationCompat.Builder NotificationCompat.Builder

Introduction

In this page you can find the example usage for android.support.v4.app NotificationCompat.Builder NotificationCompat.Builder.

Prototype

public Builder(Context context) 

Source Link

Document

Constructor.

Usage

From source file:com.andreadec.musicplayer.IndexFolderService.java

@Override
public void onCreate() {
    super.onCreate();
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setSmallIcon(android.R.drawable.stat_sys_download);
    notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
    notificationBuilder.setContentText(getResources().getString(R.string.indexingWait));
    notificationBuilder.setOngoing(true);
    notification = notificationBuilder.build();
    notificationManager.notify(Constants.NOTIFICATION_INDEXING_ONGOING, notification);
}

From source file:com.andreadec.musicplayer.PodcastEpisodeDownloaderService.java

@Override
public void onCreate() {
    super.onCreate();
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setSmallIcon(android.R.drawable.stat_sys_download);
    notificationBuilder.setOngoing(true);
    notificationBuilder.setProgress(100, 0, true);
    stopDownloadPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(STOP_DOWNLOAD_INTENT), 0);
    notificationBuilder.addAction(R.drawable.cancel, getResources().getString(R.string.stopDownload),
            stopDownloadPendingIntent);//from www .j a v a  2s .c  om
}

From source file:com.andreadec.musicplayer.IndexFolderService.java

@Override
public void onDestroy() {
    super.onDestroy();
    notificationManager.cancel(Constants.NOTIFICATION_INDEXING_ONGOING);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setSmallIcon(android.R.drawable.stat_sys_download_done);
    notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
    notificationBuilder.setContentText(getResources().getString(R.string.indexingCompleted));
    notificationManager.notify(Constants.NOTIFICATION_INDEXING_COMPLETED, notificationBuilder.build());
}

From source file:org.jitsi.android.gui.call.notification.CallNotificationManager.java

/**
 * Displays notification allowing user to control the call directly from
 * the status bar.//from ww w .  j  ava2  s  .c o  m
 * @param ctx the Android context.
 * @param callID the ID of call that will be used. The ID is managed by
 * {@link CallManager}.
 */
public synchronized void showCallNotification(Context ctx, final String callID) {
    final Call call = CallManager.getActiveCall(callID);
    if (call == null) {
        throw new IllegalArgumentException("There's no call with id: " + callID);
    }

    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(ctx)
            .setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.notificationicon);

    RemoteViews contentView = new RemoteViews(ctx.getPackageName(), R.layout.status_bar_call);

    // Sets call peer display name
    CallPeer callPeer = call.getCallPeers().next();
    contentView.setTextViewText(R.id.calleeDisplayName, callPeer.getDisplayName());

    // Binds pending intents
    setIntents(ctx, contentView, callID);

    // Sets the content view
    nBuilder.setContent(contentView);

    Notification notification = nBuilder.build();

    NotificationManager mNotificationManager = (NotificationManager) ctx
            .getSystemService(Context.NOTIFICATION_SERVICE);

    int id = SystrayServiceImpl.getGeneralNotificationId();
    mNotificationManager.notify(id, notification);

    CtrlNotificationThread notificationHandler = new CtrlNotificationThread(ctx, call, id, notification);

    handlersMap.put(callID, notificationHandler);

    call.addCallChangeListener(new CallChangeListener() {
        public void callPeerAdded(CallPeerEvent evt) {
        }

        public void callPeerRemoved(CallPeerEvent evt) {
            stopNotification(callID);
            call.removeCallChangeListener(this);
        }

        public void callStateChanged(CallChangeEvent evt) {
        }
    });

    // Starts notification update thread
    notificationHandler.start();
}

From source file:org.jitsi.android.gui.util.AndroidUtils.java

/**
 * Shows an alert dialog for the given context and a title given by
 * <tt>titleId</tt> and message given by <tt>messageId</tt>.
 *
 * @param context the android <tt>Context</tt>
 * @param notificationID the identifier of the notification to update
 * @param title the title of the message
 * @param message the message/*from w  ww .  ja  v a 2s.  c  o  m*/
 * @param date the date on which the event corresponding to the notification
 * happened
 */
public static void updateGeneralNotification(Context context, int notificationID, String title, String message,
        long date) {
    // Filter out the same subsequent notifications
    if (lastNotificationText != null && lastNotificationText.equals(message)) {
        return;
    }

    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context).setContentTitle(title)
            .setContentText(message).setWhen(date).setSmallIcon(R.drawable.notificationicon);

    nBuilder.setContentIntent(JitsiApplication.getJitsiIconIntent());
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = nBuilder.build();

    notification.flags = Notification.FLAG_ONLY_ALERT_ONCE & Notification.FLAG_FOREGROUND_SERVICE
            & Notification.FLAG_NO_CLEAR;

    // mId allows you to update the notification later on.
    mNotificationManager.notify(notificationID, notification);

    lastNotificationText = message;
}

From source file:org.jitsi.service.osgi.OSGiService.java

/**
 * Start the service in foreground and creates shows general notification
 * icon./*from w ww. j a  va 2 s.  c om*/
 */
private void showIcon() {
    //The intent to launch when the user clicks the expanded notification
    PendingIntent pendIntent = JitsiApplication.getJitsiIconIntent();

    Resources res = getResources();
    String title = res.getString(R.string.app_name);

    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this).setContentTitle(title)
            .setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.notificationicon);
    nBuilder.setContentIntent(pendIntent);

    Notification notice = nBuilder.build();
    notice.flags |= Notification.FLAG_NO_CLEAR;

    this.startForeground(GENERAL_NOTIFICATION_ID, notice);
    running_foreground = true;
}

From source file:com.andreadec.musicplayer.PodcastEpisodeDownloaderService.java

private void showErrorNotification(String msg) {
    NotificationCompat.Builder errorNotification = new NotificationCompat.Builder(this);
    errorNotification.setSmallIcon(android.R.drawable.stat_sys_download_done);
    errorNotification.setContentTitle(getResources().getString(R.string.error));
    errorNotification.setContentText(getResources().getString(R.string.podcastDownloadError) + ": " + msg);
    notificationManager.notify(Constants.NOTIFICATION_PODCAST_ITEM_DOWNLOAD_ERROR, errorNotification.build());
}

From source file:org.jitsi.impl.androidtray.AndroidPopup.java

/**
 * Builds notification and returns the builder object which can be used to
 * extend the notification.//www . j av a 2 s  . c o  m
 * @return builder object describing current notification.
 */
NotificationCompat.Builder buildNotification() {
    Context ctx = JitsiApplication.getGlobalContext();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx).setSmallIcon(smallIcon)
            .setContentTitle(popupMessage.getMessageTitle()).setContentText(getMessage()).setAutoCancel(true)// will be cancelled once clciked
            .setVibrate(new long[] {}) // no vibration
            .setSound(null); // no sound

    // Big view comes with API11
    if (AndroidUtils.hasAPI(11)) {
        Resources res = JitsiApplication.getAppResources();

        // Preferred size
        int prefWidth = (int) res.getDimension(android.R.dimen.notification_large_icon_width);
        int prefHeight = (int) res.getDimension(android.R.dimen.notification_large_icon_height);

        // Use popup icon if any
        Bitmap iconBmp = null;
        byte[] icon = popupMessage.getIcon();
        if (icon != null) {
            iconBmp = AndroidImageUtil.scaledBitmapFromBytes(icon, prefWidth, prefHeight);
        }

        // Set default avatar
        if (iconBmp == null && contact != null) {
            iconBmp = AndroidImageUtil.scaledBitmapFromResource(res, R.drawable.avatar, prefWidth, prefHeight);
        }

        if (iconBmp != null) {
            if (iconBmp.getWidth() > prefWidth || iconBmp.getHeight() > prefHeight) {
                iconBmp = Bitmap.createScaledBitmap(iconBmp, prefWidth, prefHeight, true);
            }

            builder.setLargeIcon(iconBmp);
        }

        // Build inbox style
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        onBuildInboxStyle(inboxStyle);
        builder.setStyle(inboxStyle);
    }

    return builder;
}

From source file:com.andreadec.musicplayer.MusicService.java

private void updateNotificationMessage() {
    /* Update remote control client */
    if (Build.VERSION.SDK_INT >= 14) {
        if (currentPlayingItem == null) {
            remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
        } else {//from   w w w  .  j a v  a  2s  .  c om
            if (isPlaying()) {
                remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
            } else {
                remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
            }
            RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, currentPlayingItem.getTitle());
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, currentPlayingItem.getArtist());
            metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST,
                    currentPlayingItem.getArtist());
            metadataEditor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, getDuration());
            if (currentPlayingItem.hasImage()) {
                metadataEditor.putBitmap(METADATA_KEY_ARTWORK, currentPlayingItem.getImage());
            } else {
                metadataEditor.putBitmap(METADATA_KEY_ARTWORK, icon.copy(icon.getConfig(), false));
            }
            metadataEditor.apply();
        }
    }

    /* Update notification */
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setSmallIcon(R.drawable.audio_white);
    //notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    notificationBuilder.setOngoing(true);

    int playPauseIcon = isPlaying() ? R.drawable.pause : R.drawable.play;
    //int playPauseString = isPlaying() ? R.string.pause : R.string.play;
    notificationBuilder.setContentIntent(pendingIntent);

    String notificationMessage = "";
    if (currentPlayingItem == null) {
        notificationMessage = getResources().getString(R.string.noSong);
    } else {
        if (currentPlayingItem.getArtist() != null && !currentPlayingItem.getArtist().equals(""))
            notificationMessage = currentPlayingItem.getArtist() + " - ";
        notificationMessage += currentPlayingItem.getTitle();
    }

    /*notificationBuilder.addAction(R.drawable.previous, getResources().getString(R.string.previous), previousPendingIntent);
    notificationBuilder.addAction(playPauseIcon, getResources().getString(playPauseString), playpausePendingIntent);
    notificationBuilder.addAction(R.drawable.next, getResources().getString(R.string.next), nextPendingIntent);*/
    notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
    notificationBuilder.setContentText(notificationMessage);

    notification = notificationBuilder.build();

    RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.layout_notification);

    if (currentPlayingItem == null) {
        notificationLayout.setTextViewText(R.id.textViewArtist, getString(R.string.app_name));
        notificationLayout.setTextViewText(R.id.textViewTitle, getString(R.string.noSong));
        notificationLayout.setImageViewBitmap(R.id.imageViewNotification,
                BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    } else {
        notificationLayout.setTextViewText(R.id.textViewArtist, currentPlayingItem.getArtist());
        notificationLayout.setTextViewText(R.id.textViewTitle, currentPlayingItem.getTitle());
        Bitmap image = currentPlayingItem.getImage();
        if (image != null) {
            notificationLayout.setImageViewBitmap(R.id.imageViewNotification, image);
        } else {
            notificationLayout.setImageViewBitmap(R.id.imageViewNotification,
                    BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
        }
    }
    notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationQuit, quitPendingIntent);
    notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationPrevious, previousPendingIntent);
    notificationLayout.setImageViewResource(R.id.buttonNotificationPlayPause, playPauseIcon);
    notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationPlayPause, playpausePendingIntent);
    notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationNext, nextPendingIntent);
    notification.bigContentView = notificationLayout;

    notificationManager.notify(Constants.NOTIFICATION_MAIN, notification);
}