Example usage for android.app Notification VISIBILITY_PUBLIC

List of usage examples for android.app Notification VISIBILITY_PUBLIC

Introduction

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

Prototype

int VISIBILITY_PUBLIC

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

Click Source Link

Document

Notification visibility: Show this notification in its entirety on all lockscreens.

Usage

From source file:com.onesignal.GenerateNotification.java

private static NotificationCompat.Builder getBaseNotificationCompatBuilder(JSONObject gcmBundle,
        boolean notify) {
    int notificationIcon = getSmallIconId(gcmBundle);

    int notificationDefaults = 0;

    if (OneSignal.getVibrate(currentContext))
        notificationDefaults = Notification.DEFAULT_VIBRATE;

    String message = null;/*from  ww  w.  ja  v a2  s.  c  o m*/
    try {
        message = gcmBundle.getString("alert");
    } catch (Throwable t) {
    }

    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(currentContext).setAutoCancel(true)
            .setSmallIcon(notificationIcon) // Small Icon required or notification doesn't display
            .setContentTitle(getTitle(gcmBundle))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message)).setContentText(message);
    if (notify)
        notifBuilder.setTicker(message);

    // Android 5.0 accent color to use, only works when AndroidManifest.xml is
    // targetSdkVersion >= 21
    if (gcmBundle.has("bgac")) {
        try {
            notifBuilder.setColor(new BigInteger(gcmBundle.getString("bgac"), 16).intValue());
        } catch (Throwable t) {
        } // Can throw if an old android support lib is used or parse error.
    }

    BigInteger ledColor = null;

    if (notify && gcmBundle.has("ledc")) {
        try {
            ledColor = new BigInteger(gcmBundle.getString("ledc"), 16);
            notifBuilder.setLights(ledColor.intValue(), 2000, 5000);
        } catch (Throwable t) {
            notificationDefaults |= Notification.DEFAULT_LIGHTS;
        } // Can throw if an old android support lib is used or parse error.
    } else
        notificationDefaults |= Notification.DEFAULT_LIGHTS;

    try {
        int visibility = Notification.VISIBILITY_PUBLIC;
        if (gcmBundle.has("vis"))
            visibility = Integer.parseInt(gcmBundle.getString("vis"));
        notifBuilder.setVisibility(visibility);
    } catch (Throwable t) {
    } // Can throw if an old android support lib is used or parse error

    Bitmap largeIcon = getLargeIcon(gcmBundle);
    if (largeIcon != null)
        notifBuilder.setLargeIcon(largeIcon);

    Bitmap bigPictureIcon = getBitmapIcon(gcmBundle, "bicon");
    if (bigPictureIcon != null)
        notifBuilder.setStyle(
                new NotificationCompat.BigPictureStyle().bigPicture(bigPictureIcon).setSummaryText(message));

    if (notify && OneSignal.getSoundEnabled(currentContext)) {
        Uri soundUri = getCustomSound(gcmBundle);
        if (soundUri != null)
            notifBuilder.setSound(soundUri);
        else
            notificationDefaults |= Notification.DEFAULT_SOUND;
    }

    if (!notify)
        notificationDefaults = 0;

    notifBuilder.setDefaults(notificationDefaults);

    return notifBuilder;
}

From source file:com.hmatalonga.greenhub.util.Notifier.java

public static void batteryWarningTemperature(final Context context) {
    if (sNotificationManager == null) {
        sNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }/*from w w w .  j  a v  a  2s . c om*/

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_alert_circle_white_24dp)
            .setContentTitle(context.getString(R.string.notif_battery_warning))
            .setContentText(context.getString(R.string.notif_battery_warm)).setAutoCancel(true)
            .setOngoing(false).setLights(Color.YELLOW, 500, 2000).setVibrate(new long[] { 0, 400, 1000 })
            .setPriority(SettingsUtils.fetchNotificationsPriority(context));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
    }

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, MainActivity.class);
    resultIntent.putExtra("tab", 2);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(InboxActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    // Because the ID remains unchanged, the existing notification is updated.
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    sNotificationManager.notify(Config.NOTIFICATION_TEMPERATURE_WARNING, notification);
}

From source file:com.android.nfc.beam.BeamTransferManager.java

void updateNotification() {
    Builder notBuilder = new Notification.Builder(mContext);
    notBuilder.setColor(//from  www  . j av  a2  s .co  m
            mContext.getResources().getColor(com.android.internal.R.color.system_notification_accent_color));
    notBuilder.setWhen(mStartTime);
    notBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
    String beamString;
    if (mIncoming) {
        beamString = mContext.getString(R.string.beam_progress);
    } else {
        beamString = mContext.getString(R.string.beam_outgoing);
    }
    if (mState == STATE_NEW || mState == STATE_IN_PROGRESS || mState == STATE_W4_NEXT_TRANSFER
            || mState == STATE_W4_MEDIA_SCANNER) {
        notBuilder.setAutoCancel(false);
        notBuilder.setSmallIcon(
                mIncoming ? android.R.drawable.stat_sys_download : android.R.drawable.stat_sys_upload);
        notBuilder.setTicker(beamString);
        notBuilder.setContentTitle(beamString);
        notBuilder.addAction(R.drawable.ic_menu_cancel_holo_dark, mContext.getString(R.string.cancel),
                mCancelIntent);
        float progress = 0;
        if (mTotalCount > 0) {
            float progressUnit = 1.0f / mTotalCount;
            progress = (float) mCurrentCount * progressUnit + mProgress * progressUnit;
        }
        if (mTotalCount > 0 && progress > 0) {
            notBuilder.setProgress(100, (int) (100 * progress), false);
        } else {
            notBuilder.setProgress(100, 0, true);
        }
    } else if (mState == STATE_SUCCESS) {
        notBuilder.setAutoCancel(true);
        notBuilder.setSmallIcon(mIncoming ? android.R.drawable.stat_sys_download_done
                : android.R.drawable.stat_sys_upload_done);
        notBuilder.setTicker(mContext.getString(R.string.beam_complete));
        notBuilder.setContentTitle(mContext.getString(R.string.beam_complete));

        if (mIncoming) {
            notBuilder.setContentText(mContext.getString(R.string.beam_tap_to_view));
            Intent viewIntent = buildViewIntent();
            PendingIntent contentIntent = PendingIntent.getActivity(mContext, mTransferId, viewIntent, 0, null);

            notBuilder.setContentIntent(contentIntent);
        }
    } else if (mState == STATE_FAILED) {
        notBuilder.setAutoCancel(false);
        notBuilder.setSmallIcon(mIncoming ? android.R.drawable.stat_sys_download_done
                : android.R.drawable.stat_sys_upload_done);
        notBuilder.setTicker(mContext.getString(R.string.beam_failed));
        notBuilder.setContentTitle(mContext.getString(R.string.beam_failed));
    } else if (mState == STATE_CANCELLED || mState == STATE_CANCELLING) {
        notBuilder.setAutoCancel(false);
        notBuilder.setSmallIcon(mIncoming ? android.R.drawable.stat_sys_download_done
                : android.R.drawable.stat_sys_upload_done);
        notBuilder.setTicker(mContext.getString(R.string.beam_canceled));
        notBuilder.setContentTitle(mContext.getString(R.string.beam_canceled));
    } else {
        return;
    }

    mNotificationManager.notify(null, mTransferId, notBuilder.build());
}

From source file:com.example.android.wearable.wear.wearnotifications.StandaloneMainActivity.java

private void generateBigTextStyleNotification() {

    Log.d(TAG, "generateBigTextStyleNotification()");

    // Main steps for building a BIG_TEXT_STYLE notification:
    //      0. Get your data
    //      1. Build the BIG_TEXT_STYLE
    //      2. Set up main Intent for notification
    //      3. Create additional Actions for the Notification
    //      4. Build and issue the notification

    // 0. Get your data (everything unique per Notification)
    MockDatabase.BigTextStyleReminderAppData bigTextStyleReminderAppData = MockDatabase.getBigTextStyleData();

    // 1. Build the BIG_TEXT_STYLE
    BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle()
            // Overrides ContentText in the big form of the template
            .bigText(bigTextStyleReminderAppData.getBigText())
            // Overrides ContentTitle in the big form of the template
            .setBigContentTitle(bigTextStyleReminderAppData.getBigContentTitle())
            // Summary line after the detail section in the big form of the template
            // Note: To improve readability, don't overload the user with info. If Summary Text
            // doesn't add critical information, you should skip it.
            .setSummaryText(bigTextStyleReminderAppData.getSummaryText());

    // 2. Set up main Intent for notification
    Intent mainIntent = new Intent(this, BigTextMainActivity.class);

    PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // 3. Create additional Actions (Intents) for the Notification

    // In our case, we create two additional actions: a Snooze action and a Dismiss action.

    // Snooze Action
    Intent snoozeIntent = new Intent(this, BigTextIntentService.class);
    snoozeIntent.setAction(BigTextIntentService.ACTION_SNOOZE);

    PendingIntent snoozePendingIntent = PendingIntent.getService(this, 0, snoozeIntent, 0);
    NotificationCompat.Action snoozeAction = new NotificationCompat.Action.Builder(
            R.drawable.ic_alarm_white_48dp, "Snooze", snoozePendingIntent).build();

    // Dismiss Action
    Intent dismissIntent = new Intent(this, BigTextIntentService.class);
    dismissIntent.setAction(BigTextIntentService.ACTION_DISMISS);

    PendingIntent dismissPendingIntent = PendingIntent.getService(this, 0, dismissIntent, 0);
    NotificationCompat.Action dismissAction = new NotificationCompat.Action.Builder(
            R.drawable.ic_cancel_white_48dp, "Dismiss", dismissPendingIntent).build();

    // 4. Build and issue the notification

    // Because we want this to be a new notification (not updating a previous notification), we
    // create a new Builder. Later, we use the same global builder to get back the notification
    // we built here for the snooze action, that is, canceling the notification and relaunching
    // it several seconds later.

    NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(
            getApplicationContext());/* ww  w  .  j  a  v a  2s  . co  m*/

    GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);

    notificationCompatBuilder
            // BIG_TEXT_STYLE sets title and content
            .setStyle(bigTextStyle).setContentTitle(bigTextStyleReminderAppData.getContentTitle())
            .setContentText(bigTextStyleReminderAppData.getContentText()).setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_alarm_white_48dp))
            // Set primary color (important for Wear 2.0 Notifications)
            .setColor(getResources().getColor(R.color.colorPrimary))

            .setCategory(Notification.CATEGORY_REMINDER).setPriority(Notification.PRIORITY_HIGH)

            // Shows content on the lock-screen
            .setVisibility(Notification.VISIBILITY_PUBLIC)

            // Adds additional actions specified above
            .addAction(snoozeAction).addAction(dismissAction);

    /* REPLICATE_NOTIFICATION_STYLE_CODE:
     * You can replicate Notification Style functionality on Wear 2.0 (24+) by not setting the
     * main content intent, that is, skipping the call setContentIntent(). However, you need to
     * still allow the user to open the native Wear app from the Notification itself, so you
     * add an action to launch the app.
     */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

        // Enables launching app in Wear 2.0 while keeping the old Notification Style behavior.
        NotificationCompat.Action mainAction = new NotificationCompat.Action.Builder(R.drawable.ic_launcher,
                "Open", mainPendingIntent).build();

        notificationCompatBuilder.addAction(mainAction);

    } else {
        // Wear 1.+ still functions the same, so we set the main content intent.
        notificationCompatBuilder.setContentIntent(mainPendingIntent);
    }

    Notification notification = notificationCompatBuilder.build();

    mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);

    // Close app to demonstrate notification in steam.
    finish();
}

From source file:com.example.android.mediabrowserservice.MediaNotificationManager.java

private Notification createNotification() {
    LogHelper.d(TAG, "updateNotificationMetadata. mMetadata=" + mMetadata);
    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }//from ww  w.  ja v  a2  s .co m

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);
    int playPauseButtonPosition = 0;

    // If skip to previous action is enabled
    if ((mPlaybackState.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(R.drawable.ic_skip_previous_white_24dp,
                mService.getString(R.string.label_previous), mPreviousIntent);

        // If there is a "skip to previous" button, the play/pause button will
        // be the second one. We need to keep track of it, because the MediaStyle notification
        // requires to specify the index of the buttons (actions) that should be visible
        // when in compact view.
        playPauseButtonPosition = 1;
    }

    addPlayPauseAction(notificationBuilder);

    // If skip to next action is enabled
    if ((mPlaybackState.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(R.drawable.ic_skip_next_white_24dp,
                mService.getString(R.string.label_next), mNextIntent);
    }

    MediaDescriptionCompat description = mMetadata.getDescription();

    String fetchArtUrl = null;
    Bitmap art = null;
    if (description.getIconUri() != null) {
        // This sample assumes the iconUri will be a valid URL formatted String, but
        // it can actually be any valid Android Uri formatted String.
        // async fetch the album art icon
        String artUrl = description.getIconUri().toString();
        art = AlbumArtCache.getInstance().getBigImage(artUrl);
        if (art == null) {
            fetchArtUrl = artUrl;
            // use a placeholder art while the remote art is being downloaded
            art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_default_art);
        }
    }

    notificationBuilder
            .setStyle(new NotificationCompat.MediaStyle()
                    .setShowActionsInCompactView(new int[] { playPauseButtonPosition }) // show only play/pause in compact view
                    .setMediaSession(mSessionToken))
            .setColor(mNotificationColor).setSmallIcon(R.drawable.ic_notification)
            .setVisibility(Notification.VISIBILITY_PUBLIC).setUsesChronometer(true)
            .setContentIntent(createContentIntent()).setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle()).setLargeIcon(art);

    setNotificationPlaybackState(notificationBuilder);
    if (fetchArtUrl != null) {
        fetchBitmapFromURLAsync(fetchArtUrl, notificationBuilder);
    }

    return notificationBuilder.build();
}

From source file:com.appdevper.mediaplayer.app.MediaNotificationManager.java

private Notification createNotification() {
    LogHelper.d(TAG, "updateNotificationMetadata. mMetadata=" + mMetadata);
    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }/*from w w w. j  ava  2  s.co  m*/

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);
    int playPauseButtonPosition = 0;

    // If skip to previous action is enabled
    if ((mPlaybackState.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(R.drawable.ic_skip_previous_white_24dp,
                mService.getString(R.string.label_previous), mPreviousIntent);
        playPauseButtonPosition = 1;
    }

    addPlayPauseAction(notificationBuilder);

    // If skip to next action is enabled
    if ((mPlaybackState.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(R.drawable.ic_skip_next_white_24dp,
                mService.getString(R.string.label_next), mNextIntent);
    }

    MediaDescriptionCompat description = mMetadata.getDescription();

    String fetchArtUrl = null;
    Bitmap art = null;
    if (description.getIconUri() != null) {
        String artUrl = description.getIconUri().toString();
        art = AlbumArtCache.getInstance().getBigImage(artUrl);
        if (art == null) {
            fetchArtUrl = artUrl;
            art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_default_art);
        }
    }
    notificationBuilder
            .setStyle(new NotificationCompat.MediaStyle()
                    .setShowActionsInCompactView(new int[] { playPauseButtonPosition }) // show only play/pause in compact view
                    .setMediaSession(mSessionToken))
            .setColor(mNotificationColor).setSmallIcon(R.drawable.ic_notification)
            .setVisibility(Notification.VISIBILITY_PUBLIC).setUsesChronometer(true)
            .setContentIntent(createContentIntent(description)).setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle()).setLargeIcon(art);

    if (mController != null && mController.getExtras() != null) {
        String castName = mController.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        if (castName != null) {
            String castInfo = mService.getResources().getString(R.string.casting_to_device, castName);
            notificationBuilder.setSubText(castInfo);
            notificationBuilder.addAction(R.drawable.ic_close_black_24dp,
                    mService.getString(R.string.stop_casting), mStopCastIntent);
        }
    }

    setNotificationPlaybackState(notificationBuilder);

    fetchBitmapFromURLAsync(fetchArtUrl, notificationBuilder);

    return notificationBuilder.build();
}

From source file:com.torrenttunes.android.MediaNotificationManager.java

private Notification createNotification() {
    LogHelper.d(TAG, "updateNotificationMetadata. mMetadata=" + mMetadata);
    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }/*from  www.  j av  a 2  s.c o m*/

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);
    int playPauseButtonPosition = 0;

    // If skip to previous action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(R.drawable.ic_skip_previous_white_24dp,
                mService.getString(R.string.label_previous), mPreviousIntent);

        // If there is a "skip to previous" button, the play/pause button will
        // be the second one. We need to keep track of it, because the MediaStyle notification
        // requires to specify the index of the buttons (actions) that should be visible
        // when in compact view.
        playPauseButtonPosition = 1;
    }

    addPlayPauseAction(notificationBuilder);

    // If skip to next action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(R.drawable.ic_skip_next_white_24dp,
                mService.getString(R.string.label_next), mNextIntent);
    }

    MediaDescriptionCompat description = mMetadata.getDescription();

    String fetchArtUrl = null;
    Bitmap art = null;
    if (description.getIconUri() != null) {
        // This sample assumes the iconUri will be a valid URL formatted String, but
        // it can actually be any valid Android Uri formatted String.
        // async fetch the album art icon
        String artUrl = description.getIconUri().toString();
        art = AlbumArtCache.getInstance().getBigImage(artUrl);
        if (art == null) {
            fetchArtUrl = artUrl;
            // use a placeholder art while the remote art is being downloaded
            art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_default_art);
        }
        if (art.isRecycled()) {
            art = null;
        }
    }

    notificationBuilder
            //                .setStyle(new Notification.MediaStyle()
            //                        .setShowActionsInCompactView(
            //                                new int[]{playPauseButtonPosition})  // show only play/pause in compact view
            //                        .setMediaSession(mSessionToken))
            .setColor(mNotificationColor).setSmallIcon(R.drawable.ic_notification)
            .setVisibility(Notification.VISIBILITY_PUBLIC).setUsesChronometer(true)
            .setContentIntent(createContentIntent(description)).setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle()).setLargeIcon(art);

    if (mController != null && mController.getExtras() != null) {
        String castName = mController.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        if (castName != null) {
            String castInfo = mService.getResources().getString(R.string.casting_to_device, castName);
            notificationBuilder.setSubText(castInfo);
            notificationBuilder.addAction(R.drawable.ic_close_black_24dp,
                    mService.getString(R.string.stop_casting), mStopCastIntent);
        }
    }

    setNotificationPlaybackState(notificationBuilder);
    if (fetchArtUrl != null) {
        fetchBitmapFromURLAsync(fetchArtUrl, notificationBuilder);
    }

    return notificationBuilder.build();
}

From source file:com.hmatalonga.greenhub.util.Notifier.java

public static void batteryHighTemperature(final Context context) {
    if (sNotificationManager == null) {
        sNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }/*from   w  w w .j av a 2 s .  c o  m*/

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_alert_circle_white_24dp)
            .setContentTitle(context.getString(R.string.notif_battery_hot))
            .setContentText(context.getString(R.string.notif_battery_cooldown)).setAutoCancel(true)
            .setOngoing(false).setLights(Color.RED, 500, 2000).setVibrate(new long[] { 0, 800, 1500 })
            .setPriority(SettingsUtils.fetchNotificationsPriority(context));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
    }

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, MainActivity.class);
    resultIntent.putExtra("tab", 2);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(InboxActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    // Because the ID remains unchanged, the existing notification is updated.
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    sNotificationManager.notify(Config.NOTIFICATION_TEMPERATURE_HIGH, notification);
}

From source file:audio.lisn.service.MediaNotificationManager.java

private Notification createNotification() {
    //  LogHelper.d(TAG, "updateNotificationMetadata. mMetadata=" + mMetadata);
    //        if (mMetadata == null || mPlaybackState == null) {
    //            return null;
    //        }/*  w  ww .j  av a  2 s .c  om*/

    Notification.Builder notificationBuilder = new Notification.Builder(mService);
    int playPauseButtonPosition = 1;

    // If skip to previous action is enabled
    notificationBuilder.addAction(R.drawable.ic_play_skip_previous, mService.getString(R.string.label_previous),
            mPreviousIntent);
    /*
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
    notificationBuilder.addAction(new Notification.Action( R.drawable.ic_play_skip_previous,
            mService.getString(R.string.label_previous), mPreviousIntent));
    }else{
    notificationBuilder.addAction(R.drawable.ic_play_skip_previous,
            mService.getString(R.string.label_previous), mPreviousIntent);
    }
    */

    // If there is a "skip to previous" button, the play/pause button will
    // be the second one. We need to keep track of it, because the MediaStyle notification
    // requires to specify the index of the buttons (actions) that should be visible
    // when in compact view.

    //
    addPlayPauseAction(notificationBuilder);

    // If skip to next action is enabled
    notificationBuilder.addAction(R.drawable.ic_play_skip_next, mService.getString(R.string.label_next),
            mNextIntent);
    /*
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
    notificationBuilder.addAction(new Notification.Action(R.drawable.ic_play_skip_next,
            mService.getString(R.string.label_next), mNextIntent));
    }else{
    notificationBuilder.addAction(R.drawable.ic_play_skip_next,
            mService.getString(R.string.label_next), mNextIntent);
    }
    */
    AudioBook audioBook = AppController.getInstance().getCurrentAudioBook();

    Bitmap art = null;
    /*
    String img_path = AppUtils.getDataDirectory(mService.getApplicationContext())
        + audioBook.getBook_id()+ File.separator+"book_cover.jpg";
            
        File imgFile = new  File(img_path);
            
    if(imgFile.exists()) {
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    art = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions);
    }else{
    art = BitmapFactory.decodeResource(mService.getResources(),
            R.drawable.ui_bg_logo);
    }
    */
    art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_notification_large);

    notificationBuilder.setSmallIcon(R.drawable.ic_notification).setUsesChronometer(true)
            .setContentIntent(createContentIntent()).setContentTitle(audioBook.getEnglish_title())
            .setDeleteIntent(mDeleteIntent)
            .setContentText(AppController.getInstance().getPlayerControllerTitle()).setLargeIcon(art);

    if (Build.VERSION.SDK_INT >= 21) {
        notificationBuilder.setStyle(
                new Notification.MediaStyle().setShowActionsInCompactView(new int[] { playPauseButtonPosition }) // show only play/pause in compact view

        ).setVisibility(Notification.VISIBILITY_PUBLIC).setColor(mNotificationColor)

        ;
    }

    setNotificationPlaybackState(notificationBuilder);

    return notificationBuilder.build();
}

From source file:com.example.android.wearable.wear.wearnotifications.MainActivity.java

private void generateBigTextStyleNotification() {

    Log.d(TAG, "generateBigTextStyleNotification()");

    // Main steps for building a BIG_TEXT_STYLE notification:
    //      0. Get your data
    //      1. Build the BIG_TEXT_STYLE
    //      2. Set up main Intent for notification
    //      3. Create additional Actions for the Notification
    //      4. Build and issue the notification

    // 0. Get your data (everything unique per Notification)
    MockDatabase.BigTextStyleReminderAppData bigTextStyleReminderAppData = MockDatabase.getBigTextStyleData();

    // 1. Build the BIG_TEXT_STYLE
    BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle()
            // Overrides ContentText in the big form of the template
            .bigText(bigTextStyleReminderAppData.getBigText())
            // Overrides ContentTitle in the big form of the template
            .setBigContentTitle(bigTextStyleReminderAppData.getBigContentTitle())
            // Summary line after the detail section in the big form of the template
            // Note: To improve readability, don't overload the user with info. If Summary Text
            // doesn't add critical information, you should skip it.
            .setSummaryText(bigTextStyleReminderAppData.getSummaryText());

    // 2. Set up main Intent for notification
    Intent notifyIntent = new Intent(this, BigTextMainActivity.class);

    // When creating your Intent, you need to take into account the back state, i.e., what
    // happens after your Activity launches and the user presses the back button.

    // There are two options:
    //      1. Regular activity - You're starting an Activity that's part of the application's
    //      normal workflow.

    //      2. Special activity - The user only sees this Activity if it's started from a
    //      notification. In a sense, the Activity extends the notification by providing
    //      information that would be hard to display in the notification itself.

    // For the BIG_TEXT_STYLE notification, we will consider the activity launched by the main
    // Intent as a special activity, so we will follow option 2.

    // For an example of option 1, check either the MESSAGING_STYLE or BIG_PICTURE_STYLE
    // examples.//from w  w  w  .  j ava 2s. co m

    // For more information, check out our dev article:
    // https://developer.android.com/training/notify-user/navigation.html

    // Sets the Activity to start in a new, empty task
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // 3. Create additional Actions (Intents) for the Notification

    // In our case, we create two additional actions: a Snooze action and a Dismiss action
    // Snooze Action
    Intent snoozeIntent = new Intent(this, BigTextIntentService.class);
    snoozeIntent.setAction(BigTextIntentService.ACTION_SNOOZE);

    PendingIntent snoozePendingIntent = PendingIntent.getService(this, 0, snoozeIntent, 0);
    NotificationCompat.Action snoozeAction = new NotificationCompat.Action.Builder(
            R.drawable.ic_alarm_white_48dp, "Snooze", snoozePendingIntent).build();

    // Dismiss Action
    Intent dismissIntent = new Intent(this, BigTextIntentService.class);
    dismissIntent.setAction(BigTextIntentService.ACTION_DISMISS);

    PendingIntent dismissPendingIntent = PendingIntent.getService(this, 0, dismissIntent, 0);
    NotificationCompat.Action dismissAction = new NotificationCompat.Action.Builder(
            R.drawable.ic_cancel_white_48dp, "Dismiss", dismissPendingIntent).build();

    // 4. Build and issue the notification

    // Because we want this to be a new notification (not updating a previous notification), we
    // create a new Builder. Later, we use the same global builder to get back the notification
    // we built here for the snooze action, that is, canceling the notification and relaunching
    // it several seconds later.

    NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(
            getApplicationContext());

    GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);

    Notification notification = notificationCompatBuilder
            // BIG_TEXT_STYLE sets title and content for API 16 (4.1 and after)
            .setStyle(bigTextStyle)
            // Title for API <16 (4.0 and below) devices
            .setContentTitle(bigTextStyleReminderAppData.getContentTitle())
            // Content for API <24 (7.0 and below) devices
            .setContentText(bigTextStyleReminderAppData.getContentText()).setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_alarm_white_48dp))
            .setContentIntent(notifyPendingIntent)
            // Set primary color (important for Wear 2.0 Notifications)
            .setColor(getResources().getColor(R.color.colorPrimary))

            // SIDE NOTE: Auto-bundling is enabled for 4 or more notifications on API 24+ (N+)
            // devices and all Android Wear devices. If you have more than one notification and
            // you prefer a different summary notification, set a group key and create a
            // summary notification via
            // .setGroupSummary(true)
            // .setGroup(GROUP_KEY_YOUR_NAME_HERE)

            .setCategory(Notification.CATEGORY_REMINDER).setPriority(Notification.PRIORITY_HIGH)

            // Shows content on the lock-screen
            .setVisibility(Notification.VISIBILITY_PUBLIC)

            // Adds additional actions specified above
            .addAction(snoozeAction).addAction(dismissAction)

            .build();

    mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);
}