Example usage for android.app Notification PRIORITY_MAX

List of usage examples for android.app Notification PRIORITY_MAX

Introduction

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

Prototype

int PRIORITY_MAX

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

Click Source Link

Document

Highest #priority , for your application's most important items that require the user's prompt attention or input.

Usage

From source file:kr.ds.myfoodpdx.MyGcmListenerService.java

private void sendNotification(String message, Bitmap bitmap) {
    Intent intent = new Intent(this, IntroActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    boolean isvibrate = SharedPreference.getBooleanSharedPreference(getApplicationContext(), "isvibrate");
    NotificationCompat.Builder notificationBuilder;
    if (isvibrate) {
        notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(getResources().getString(R.string.app_name)).setSmallIcon(R.mipmap.icon)
                .setContentText(message).setAutoCancel(true).setSound(defaultSoundUri)
                .setPriority(Notification.PRIORITY_MAX).setContentIntent(pendingIntent);
    } else {//from  w w w  .  j  av a  2s  .c  o  m
        notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(getResources().getString(R.string.app_name)).setSmallIcon(R.mipmap.icon)
                .setContentText(message).setAutoCancel(true).setVibrate(new long[] { 0 })
                .setPriority(Notification.PRIORITY_MAX).setContentIntent(pendingIntent);
    }
    if (bitmap != null) {
        NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
        style.setBigContentTitle(getString(R.string.app_name));
        style.setSummaryText(message);
        style.bigPicture(bitmap);
        notificationBuilder.setStyle(style);
    }

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

    notificationManager.notify(UniqueID.getRandomNumber(1000), notificationBuilder.build());
}

From source file:com.hedgehog.smdb.ActionBarControlScrollViewActivity.java

private void showNotification() {
    Notification notification = null;
    Intent toLive = new Intent(this, ActionBarControlScrollViewActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), toLive, 0);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        notification = new Notification.Builder(this).setContentTitle("Time to Check the new Show!")
                .setContentInfo("").setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pIntent)
                .setPriority(Notification.PRIORITY_MAX).setDefaults(Notification.DEFAULT_VIBRATE)
                .setAutoCancel(true).setOnlyAlertOnce(true)
                //                    .setTicker("Show running!")
                //                    .addAction(R.mipmap.ic_launcher, "View", pIntent)
                .build();//w ww .ja v  a2 s  .c  o m
    }
    NotificationManager NM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

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

From source file:com.dotcom.jamaat.fcm.GCMListenerService.java

/**
 * Create and show a simple notification containing the received GCM
 * message.//from   w  ww . ja  va  2s.co m
 * <p/>
 * <p/>
 * GCM message received.
 */
public void displayNotificationInNotificationBar(Map data) {
    try {
        String messageData = data.get("message").toString();
        int notifyID = Constants.NOTIFICATION_ID;
        //String eData = data.getString("extra_data");
        String message = "";
        if (!TextUtils.isEmpty(messageData)) {
            JSONObject extra_data = new JSONObject(messageData);
            message = extra_data.optString("message");
        }

        // This intent is fired when notification is clicked

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

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Intent intent;
        String messages = SharedPreferencesManager.getStringPreference("notificationMessage", null);
        if (messages != null && !messages.isEmpty()) {
            messages = message + ";" + messages;
            SharedPreferencesManager.setPreference("notificationMessage", messages);

            intent = new Intent(this, NotificationActivity.class);
        } else {
            SharedPreferencesManager.setPreference("notificationMessage", message);

            intent = new Intent(this, NotificationActivity.class);
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        String currentMessages = SharedPreferencesManager.getStringPreference("notificationMessage", null);
        String[] messagesArray = currentMessages.split(";");

        int count = messagesArray.length;
        if (messagesArray.length == 0) {

            notificationManager.cancel(notifyID);
            return;
        }

        final String GROUP_KEY_MESSAGES = "group_key_messages";

        // Group notification that will be visible on the phone
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setTicker(getString(R.string.app_name)).setDefaults(Notification.DEFAULT_ALL)
                .setContentTitle(getString(R.string.app_name)).setSound(defaultSoundUri).setAutoCancel(true)
                .setOnlyAlertOnce(false).setPriority(Notification.PRIORITY_MAX)
                //               .setOngoing(true)
                //               .setWhen( System.currentTimeMillis() )
                .setContentIntent(pendingIntent).setSmallIcon(R.mipmap.ic_launcher).setGroup(GROUP_KEY_MESSAGES)
                .setGroupSummary(true).build();

        NotificationCompat.Style style;
        if (count > 1) {
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            style = inboxStyle;

            mBuilder.setContentTitle(getString(R.string.app_name));

            for (String r : messagesArray) {
                inboxStyle.addLine(r);
            }
            mBuilder.setContentText(count + " new messages");
            inboxStyle.setBigContentTitle(getString(R.string.app_name));
            inboxStyle.setSummaryText(count + " new messages");
        } else {
            NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
            style = bigTextStyle;

            //            bigTextStyle.setBigContentTitle(messagesArray[0].substring(0, 10).concat(" ..."));
            bigTextStyle.setBigContentTitle(getString(R.string.app_name));
            mBuilder.setContentText(message);
            bigTextStyle.bigText(message);
            //            bigTextStyle.setSummaryText(count + " new event");
        }
        mBuilder.setStyle(style);

        NotificationManagerCompat notificationManager1 = NotificationManagerCompat.from(this);
        notificationManager1.notify(notifyID, mBuilder.build());

    } catch (Exception ex) {

    }
}

From source file:net.networksaremadeofstring.rhybudd.Notifications.java

public static void SendGCMNotification(ZenossEvent Event, Context context) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);

    Time now = new Time();
    now.setToNow();//from  w  ww .  ja  v a 2 s  . c o  m

    //We don't need to overwhelm the user with their notification sound / vibrator
    if ((now.toMillis(true) - PreferenceManager.getDefaultSharedPreferences(context).getLong("lastCheck",
            now.toMillis(true))) < 3000) {
        //Log.e("SendGCMNotification", "Not publishing a notification due to stampede control");
        return;
    }

    Intent notificationIntent = new Intent(context, ViewZenossEventsListActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("forceRefresh", true);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    Uri soundURI = null;
    try {
        if (settings.getBoolean("notificationSound", true)) {
            if (settings.getString("notificationSoundChoice", "").equals("")) {
                soundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            } else {
                try {
                    soundURI = Uri.parse(settings.getString("notificationSoundChoice", ""));
                } catch (Exception e) {
                    soundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                }
            }
        } else {
            soundURI = null;
        }
    } catch (Exception e) {

    }

    String notifTitle = "New Events Received";
    int notifPriority = Notification.PRIORITY_DEFAULT;
    //int AlertType = NOTIFICATION_GCM_GENERIC;

    try {
        if (Event.getSeverity().equals("5")) {
            notifTitle = context.getString(R.string.CriticalNotificationTitle);
            notifPriority = Notification.PRIORITY_MAX;
            //AlertType = NOTIFICATION_GCM_CRITICAL;
        } else if (Event.getSeverity().equals("4")) {
            notifTitle = context.getString(R.string.ErrorNotificationTitle);
            notifPriority = Notification.PRIORITY_HIGH;
            //AlertType = NOTIFICATION_GCM_ERROR;
        } else if (Event.getSeverity().equals("3")) {
            notifTitle = context.getString(R.string.WarnNotificationTitle);
            notifPriority = Notification.PRIORITY_DEFAULT;
            //AlertType = NOTIFICATION_GCM_WARNING;
        } else if (Event.getSeverity().equals("2")) {
            notifTitle = context.getString(R.string.InfoNotificationTitle);
            notifPriority = Notification.PRIORITY_LOW;
            //AlertType = NOTIFICATION_GCM_INFO;
        } else if (Event.getSeverity().equals("1")) {
            notifTitle = context.getString(R.string.DebugNotificationTitle);
            notifPriority = Notification.PRIORITY_MIN;
            //AlertType = NOTIFICATION_GCM_DEBUG;
        }
    } catch (Exception e) {

    }

    long[] vibrate = { 0, 100, 200, 300 };

    try {
        AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        //Log.e("audio.getRingerMode()",Integer.toString(audio.getRingerMode()));
        switch (audio.getRingerMode()) {
        case AudioManager.RINGER_MODE_SILENT:
            //Do nothing to fix GitHub issue #13
            //Log.e("AudioManager","Doing nothing because we are silent");
            vibrate = new long[] { 0, 0 };
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    Intent broadcastMassAck = new Intent();
    broadcastMassAck.setAction(MassAcknowledgeReceiver.BROADCAST_ACTION);
    PendingIntent pBroadcastMassAck = PendingIntent.getBroadcast(context, 0, broadcastMassAck, 0);

    if (Build.VERSION.SDK_INT >= 16) {
        Notification noti = new Notification.BigTextStyle(new Notification.Builder(context)
                .setContentTitle(notifTitle).setPriority(notifPriority).setAutoCancel(true).setSound(soundURI)
                .setVibrate(vibrate).setContentText(Event.getDevice()).setContentIntent(contentIntent)
                .addAction(R.drawable.ic_action_resolve_all, "Acknowledge all Events", pBroadcastMassAck)
                .setSmallIcon(R.drawable.ic_stat_alert)).bigText(
                        Event.getSummary() + "\r\n" + Event.getComponentText() + "\r\n" + Event.geteventClass())
                        .build();

        if (settings.getBoolean("notificationSoundInsistent", false))
            noti.flags |= Notification.FLAG_INSISTENT;

        noti.tickerText = notifTitle;

        NotificationManager mNM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNM.notify(NOTIFICATION_GCM_GENERIC, noti);
    } else {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_stat_alert).setContentTitle(notifTitle)
                .setContentText(Event.getDevice() + ": " + Event.getSummary()).setContentIntent(contentIntent)
                .setSound(soundURI).setVibrate(vibrate)
                .addAction(R.drawable.ic_action_resolve_all, "Acknowledge all Events", pBroadcastMassAck)
                .setAutoCancel(true).setPriority(notifPriority);

        NotificationManager mNM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNM.notify(NOTIFICATION_GCM_GENERIC, mBuilder.build());
    }
}

From source file:org.alpine_toolkit.AlpineToolkitService.java

private Notification build_notification() {
    Intent notification_intent = new Intent(this, AlpineToolkitActivity.class);
    // notification_intent.setAction(Constants.ACTION.MAIN);
    // notification_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pending_intent = PendingIntent.getActivity(this, 0, notification_intent, 0);

    Intent stop_intent = new Intent(this, AlpineToolkitService.class);
    stop_intent.setAction(STOP_ACTION);// w  w  w.  ja v  a 2  s .  co m
    PendingIntent pending_stop_intent = PendingIntent.getService(this, 0, stop_intent, 0);

    int icon_id = getResources().getIdentifier("icon", "drawable", getPackageName());
    int icon_transparent_id = getResources().getIdentifier("icon_transparent", "drawable", getPackageName());
    Bitmap icon = BitmapFactory.decodeResource(getResources(), icon_id);

    // return new NotificationCompat.Builder(this)
    return new Notification.Builder(this).setPriority(Notification.PRIORITY_MAX)
            .setContentTitle("Alpine Toolkit Service").setTicker("Alpine Toolkit Service is started") // Set the text that is displayed in the status bar when the notification first arrives.
            .setContentText("Hello!").setSmallIcon(icon_transparent_id)
            .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)).setContentIntent(pending_intent) // Supply a PendingIntent to send when the notification is clicked.
            .setOngoing(true) //  Ongoing notifications cannot be dismissed by the user
            .addAction(android.R.drawable.ic_media_pause, "Stop", pending_stop_intent) // ic_media_stop
            .build();
}

From source file:com.waz.zclient.controllers.notifications.NotificationsController.java

private void showCallNotification(ActiveChannel activeChannel, int id, boolean noClear) {
    Notification notification = getCallNotification(activeChannel);
    notification.priority = Notification.PRIORITY_MAX;
    if (noClear) {
        notification.flags |= Notification.FLAG_NO_CLEAR;
    }/*w ww . j  a v  a 2  s  .  c om*/
    notificationManager.notify(id, notification);
}

From source file:com.sourceauditor.sahomemonitor.GcmIntentService.java

private void sendNotification(String msg, Bundle extras) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notifyMainIntent = buildIntentForMainActifity(extras);

    // Figure out how to add extras for the URL and message
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notifyMainIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.home_small).setContentTitle("Home Monitor Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setExtras(extras).setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    mBuilder.setLights(0xFF0000, 500, 500);
    long[] pattern = new long[] { 0, 500, 0, 500, 0, 500, 0, 500, 0, 500 };
    mBuilder.setVibrate(pattern);//from ww w . j  a  v a 2  s .  c  o m
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mBuilder.setSound(soundUri);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

From source file:com.android.deskclock.data.StopwatchNotificationBuilderN.java

@Override
public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) {
    @StringRes/*from w  ww .j a  va2s . c o  m*/
    final int eventLabel = R.string.label_notification;

    // Intent to load the app when the notification is tapped.
    final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH)
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

    final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    // Compute some values required below.
    final boolean running = stopwatch.isRunning();
    final String pname = context.getPackageName();
    final Resources res = context.getResources();
    final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime();

    final RemoteViews content = new RemoteViews(pname, R.layout.chronometer_notif_content);
    content.setChronometer(R.id.chronometer, base, null, running);

    final List<Notification.Action> actions = new ArrayList<>(2);

    if (running) {
        // Left button: Pause
        final Intent pause = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

        final Icon icon1 = Icon.createWithResource(context, R.drawable.ic_pause_24dp);
        final CharSequence title1 = res.getText(R.string.sw_pause_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause);
        actions.add(new Notification.Action.Builder(icon1, title1, intent1).build());

        // Right button: Add Lap
        if (DataModel.getDataModel().canAddMoreLaps()) {
            final Intent lap = new Intent(context, StopwatchService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

            final Icon icon2 = Icon.createWithResource(context, R.drawable.ic_sw_lap_24dp);
            final CharSequence title2 = res.getText(R.string.sw_lap_button);
            final PendingIntent intent2 = Utils.pendingServiceIntent(context, lap);
            actions.add(new Notification.Action.Builder(icon2, title2, intent2).build());
        }

        // Show the current lap number if any laps have been recorded.
        final int lapCount = DataModel.getDataModel().getLaps().size();
        if (lapCount > 0) {
            final int lapNumber = lapCount + 1;
            final String lap = res.getString(R.string.sw_notification_lap_number, lapNumber);
            content.setTextViewText(R.id.state, lap);
            content.setViewVisibility(R.id.state, VISIBLE);
        } else {
            content.setViewVisibility(R.id.state, GONE);
        }
    } else {
        // Left button: Start
        final Intent start = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

        final Icon icon1 = Icon.createWithResource(context, R.drawable.ic_start_24dp);
        final CharSequence title1 = res.getText(R.string.sw_start_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, start);
        actions.add(new Notification.Action.Builder(icon1, title1, intent1).build());

        // Right button: Reset (dismisses notification and resets stopwatch)
        final Intent reset = new Intent(context, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

        final Icon icon2 = Icon.createWithResource(context, R.drawable.ic_reset_24dp);
        final CharSequence title2 = res.getText(R.string.sw_reset_button);
        final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset);
        actions.add(new Notification.Action.Builder(icon2, title2, intent2).build());

        // Indicate the stopwatch is paused.
        content.setTextViewText(R.id.state, res.getString(R.string.swn_paused));
        content.setViewVisibility(R.id.state, VISIBLE);
    }

    // Swipe away will reset the stopwatch without bringing forward the app.
    final Intent reset = new Intent(context, StopwatchService.class)
            .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

    return new Notification.Builder(context).setLocalOnly(true).setOngoing(running)
            .setCustomContentView(content).setContentIntent(pendingShowApp).setAutoCancel(stopwatch.isPaused())
            .setPriority(Notification.PRIORITY_MAX).setSmallIcon(R.drawable.stat_notify_stopwatch)
            .setGroup(nm.getStopwatchNotificationGroupKey())
            .setStyle(new Notification.DecoratedCustomViewStyle())
            .setDeleteIntent(Utils.pendingServiceIntent(context, reset))
            .setActions(actions.toArray(new Notification.Action[actions.size()]))
            .setColor(ContextCompat.getColor(context, R.color.default_background)).build();
}

From source file:net.digitalfeed.pdroidalternative.intenthandler.PackageChangeHandler.java

private void displayNotification(Context context, NotificationType notificationType, String packageName,
        String label) {/*from w ww .  jav a2s.c  o  m*/
    //This pattern is essentially taken from
    //https://developer.android.com/guide/topics/ui/notifiers/notifications.html

    Resources res = context.getResources();

    //TODO: Fix the icon in the notification bar            
    Notification.Builder builder = new Notification.Builder(context).setPriority(Notification.PRIORITY_MAX)
            .setSmallIcon(R.drawable.notification_icon);
    //.setLargeIcon(res.getDrawable(R.drawable.allow_icon))

    String appLabel = DBInterface.getInstance(context).getApplicationLabel(packageName);
    Log.d("PDroidAlternative", "new packagename is " + packageName);
    Log.d("PDroidAlternative", "app label is " + appLabel);
    switch (notificationType) {
    case newinstall:
        builder.setContentTitle(appLabel + " " + res.getString(R.string.notification_newinstall_title))
                .setContentText(res.getString(R.string.notification_newinstall_text));
        break;
    case update:
        builder.setContentTitle(appLabel + " " + res.getString(R.string.notification_update_title))
                .setContentText(res.getString(R.string.notification_update_text));
        break;
    }

    Intent packageDetailIntent = new Intent(context, AppDetailActivity.class);
    packageDetailIntent.putExtra(AppDetailActivity.BUNDLE_PACKAGE_NAME, packageName);
    packageDetailIntent.putExtra(AppDetailActivity.BUNDLE_IN_APP, false);
    packageDetailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK & Intent.FLAG_ACTIVITY_CLEAR_TASK);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(AppDetailActivity.class);
    stackBuilder.addNextIntent(packageDetailIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);

    Notification builtNotification = builder.build();
    builtNotification.flags = builtNotification.flags | Notification.FLAG_AUTO_CANCEL
            | Notification.FLAG_NO_CLEAR;

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, builtNotification);
}

From source file:com.androidinspain.deskclock.data.StopwatchNotificationBuilder.java

public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) {
    @StringRes/*from   ww  w.j a  v a  2 s .c o m*/
    final int eventLabel = com.androidinspain.deskclock.R.string.label_notification;

    // Intent to load the app when the notification is tapped.
    final Intent showApp = new Intent(context, StopwatchService.class)
            .setAction(StopwatchService.ACTION_SHOW_STOPWATCH).putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

    final PendingIntent pendingShowApp = PendingIntent.getService(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    // Compute some values required below.
    final boolean running = stopwatch.isRunning();
    final String pname = context.getPackageName();
    final Resources res = context.getResources();
    final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime();

    final RemoteViews content = new RemoteViews(pname,
            com.androidinspain.deskclock.R.layout.chronometer_notif_content);
    content.setChronometer(com.androidinspain.deskclock.R.id.chronometer, base, null, running);

    final List<Action> actions = new ArrayList<>(2);

    if (running) {
        // Left button: Pause
        final Intent pause = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_PAUSE_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon1 = com.androidinspain.deskclock.R.drawable.ic_pause_24dp;
        final CharSequence title1 = res.getText(com.androidinspain.deskclock.R.string.sw_pause_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause);
        actions.add(new Action.Builder(icon1, title1, intent1).build());

        // Right button: Add Lap
        if (DataModel.getDataModel().canAddMoreLaps()) {
            final Intent lap = new Intent(context, StopwatchService.class)
                    .setAction(StopwatchService.ACTION_LAP_STOPWATCH)
                    .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

            @DrawableRes
            final int icon2 = com.androidinspain.deskclock.R.drawable.ic_sw_lap_24dp;
            final CharSequence title2 = res.getText(com.androidinspain.deskclock.R.string.sw_lap_button);
            final PendingIntent intent2 = Utils.pendingServiceIntent(context, lap);
            actions.add(new Action.Builder(icon2, title2, intent2).build());
        }

        // Show the current lap number if any laps have been recorded.
        final int lapCount = DataModel.getDataModel().getLaps().size();
        if (lapCount > 0) {
            final int lapNumber = lapCount + 1;
            final String lap = res.getString(com.androidinspain.deskclock.R.string.sw_notification_lap_number,
                    lapNumber);
            content.setTextViewText(com.androidinspain.deskclock.R.id.state, lap);
            content.setViewVisibility(com.androidinspain.deskclock.R.id.state, VISIBLE);
        } else {
            content.setViewVisibility(com.androidinspain.deskclock.R.id.state, GONE);
        }
    } else {
        // Left button: Start
        final Intent start = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_START_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon1 = com.androidinspain.deskclock.R.drawable.ic_start_24dp;
        final CharSequence title1 = res.getText(com.androidinspain.deskclock.R.string.sw_start_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, start);
        actions.add(new Action.Builder(icon1, title1, intent1).build());

        // Right button: Reset (dismisses notification and resets stopwatch)
        final Intent reset = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_RESET_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon2 = com.androidinspain.deskclock.R.drawable.ic_reset_24dp;
        final CharSequence title2 = res.getText(com.androidinspain.deskclock.R.string.sw_reset_button);
        final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset);
        actions.add(new Action.Builder(icon2, title2, intent2).build());

        // Indicate the stopwatch is paused.
        content.setTextViewText(com.androidinspain.deskclock.R.id.state,
                res.getString(com.androidinspain.deskclock.R.string.swn_paused));
        content.setViewVisibility(com.androidinspain.deskclock.R.id.state, VISIBLE);
    }

    final Builder notification = new NotificationCompat.Builder(context).setLocalOnly(true).setOngoing(running)
            .setCustomContentView(content).setContentIntent(pendingShowApp).setAutoCancel(stopwatch.isPaused())
            .setPriority(Notification.PRIORITY_MAX)
            .setSmallIcon(com.androidinspain.deskclock.R.drawable.stat_notify_stopwatch)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setColor(ContextCompat.getColor(context, com.androidinspain.deskclock.R.color.default_background));

    if (Utils.isNOrLater()) {
        notification.setGroup(nm.getStopwatchNotificationGroupKey());
    }

    for (Action action : actions) {
        notification.addAction(action);
    }

    return notification.build();
}