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.hmatalonga.greenhub.util.Notifier.java

public static void newMessageAlert(final Context context) {
    if (sNotificationManager == null) {
        sNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }//from w w  w. j  ava 2s .  co m

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_email_white_24dp)
            .setContentTitle(context.getString(R.string.notif_new_message))
            .setContentText(context.getString(R.string.notif_open_inbox)).setAutoCancel(true).setOngoing(false)
            .setLights(Color.GREEN, 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, InboxActivity.class);

    // 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_MESSAGE_NEW, notification);
}

From source file:pylapp.smoothclicker.android.notifiers.StatusBarNotifier.java

/**
 * Makes a unmovable notification with a dedicated LED color.
 * This notification is an "on going" one, and should be displayed will the app is clicking.
 *
 * @param type - The notification type//from  w ww.j ava2  s.c o  m
 * @param params -
 *               <ul>
 *                  <li>For CLICK_MADE :params[0] for the X coordinate, params[1] for the Y coordinate</li>
 *                  <li>For COUNT_DOWN :params[0] for the leaving time to display</li>
 *                  <li>Nothing otherwise</li>
 *               </ul>
 */
public void makeNotification(NotificationTypes type, long... params) {

    Logger.d(LOG_TAG, "New notification: " + type);

    NotificationCompat.Builder b = new NotificationCompat.Builder(mContext);
    b.setSmallIcon(R.drawable.notification_icon);
    b.setContentTitle(mContext.getString(R.string.notif_content_title));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        b.setVisibility(Notification.VISIBILITY_PUBLIC);

    if (type != NotificationTypes.CLICK_MADE && type != NotificationTypes.CLICKS_ON_GOING_BY_SERVICE) {
        Intent activityToStartOnClick = new Intent(mContext, ClickerActivity.class);
        activityToStartOnClick.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pi = PendingIntent.getActivity(mContext, 0, activityToStartOnClick, 0);
        b.setContentIntent(pi);
    }

    NotificationManager nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification n = null;

    switch (type) {
    case CLICKS_ON_GOING_BY_APP:
        b.setContentText(mContext.getString(R.string.notif_content_text_clicks_on_going_app));
        b.setProgress(0, 0, true);
        b.setLights(0xff9c27b0, 1000, 500);
        n = b.build();
        n.flags |= Notification.FLAG_NO_CLEAR;
        n.flags |= Notification.FLAG_SHOW_LIGHTS;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
            n.flags |= Notification.FLAG_LOCAL_ONLY;
        nm.notify(NOTIF_CLICK_PROCESS_ON_GOING_BY_APP, n);
        break;
    case CLICKS_ON_GOING_STANDALONE:
        b.setContentText(mContext.getString(R.string.notif_content_text_clicks_on_going_standalone));
        b.setProgress(0, 0, true);
        b.setLights(0xff9c27b0, 1000, 500);
        n = b.build();
        n.flags |= Notification.FLAG_NO_CLEAR;
        n.flags |= Notification.FLAG_SHOW_LIGHTS;
        n.flags |= Notification.FLAG_LOCAL_ONLY;
        nm.notify(NOTIF_CLICK_PROCESS_ON_GOING_STANDALONE, n);
        break;
    case CLICKS_ON_GOING_BY_SERVICE:
        b.setContentText(mContext.getString(R.string.notif_content_text_clicks_on_going_service));
        b.setProgress(0, 0, true);
        b.setLights(0xff9c27b0, 1000, 500);
        n = b.build();
        n.flags |= Notification.FLAG_NO_CLEAR;
        n.flags |= Notification.FLAG_SHOW_LIGHTS;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
            n.flags |= Notification.FLAG_LOCAL_ONLY;
        nm.notify(NOTIF_CLICK_PROCESS_ON_GOING_BY_SERVICE, n);
        break;
    case CLICKS_STOPPED:
        b.setContentText(mContext.getString(R.string.notif_content_text_clicks_stop));
        n = b.build();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
            n.flags |= Notification.FLAG_LOCAL_ONLY;
        nm.notify(NOTIF_CLICK_PROCESS_STOPPED, n);
        break;
    case CLICKS_OVER:
        b.setContentText(mContext.getString(R.string.notif_content_text_clicks_over));
        n = b.build();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
            n.flags |= Notification.FLAG_LOCAL_ONLY;
        nm.notify(NOTIF_CLICK_PROCESS_OVER, n);
        break;
    case WATCH_OVER:
        b.setContentText(mContext.getString(R.string.notif_content_text_watch_over));
        n = b.build();
        n.flags |= Notification.FLAG_LOCAL_ONLY;
        nm.notify(NOTIF_WATCH_PROCESS_OVER, n);
        break;
    case CLICK_MADE:
        StringBuilder sb = new StringBuilder();
        sb.append(mContext.getString(R.string.notif_content_text_click_made));
        if (params != null && params.length == 2) {
            sb.append(" : ").append(params[0]).append(" / ").append(params[1]);
        }
        b.setContentText(sb.toString());
        n = b.build();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
            n.flags |= Notification.FLAG_LOCAL_ONLY;
        nm.notify(NOTIF_CLICK_MADE, n);
        break;
    case SU_GRANTED:
        b.setContentText(mContext.getString(R.string.notif_content_text_su_granted));
        n = b.build();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
            n.flags |= Notification.FLAG_LOCAL_ONLY;
        nm.notify(NOTIF_SU_GRANTED, n);
        break;
    case COUNT_DOWN:
        if (params != null && params.length == 1) {
            b.setContentText(mContext.getString(R.string.notif_content_text_countdown) + " " + params[0]);
        } else {
            b.setContentText(mContext.getString(R.string.notif_content_text_countdown));
        }
        n = b.build();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
            n.flags |= Notification.FLAG_NO_CLEAR;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
            n.flags |= Notification.FLAG_LOCAL_ONLY;
        nm.notify(NOTIF_COUNT_DOWN, n);
        break;
    }

}

From source file:com.oceansky.yellow.service.ServiceNotification.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void buildNotification() {
    if (mCurrentSong == null) {
        return;/* w ww.  j a  v a  2s  . com*/
    }

    buildRemoteViews();
    buildBaseNotification();

    // And get the notification object
    mNotification = mBuilder.build();

    // Add the expanded controls
    mNotification.bigContentView = mExpandedTemplate;

    if (Utils.hasLollipop()) {
        mNotification.category = Notification.CATEGORY_SERVICE;
        mNotification.visibility = Notification.VISIBILITY_PUBLIC;
    }

    // Update fields
    final ProviderAggregator aggregator = ProviderAggregator.getDefault();
    if (mCurrentSong != null) {
        if (mCurrentSong.isLoaded()) {
            final Artist artist = mCurrentSong.getArtist() != null
                    ? aggregator.retrieveArtist(mCurrentSong.getArtist(), mCurrentSong.getProvider())
                    : null;
            final Album album = mCurrentSong.getAlbum() != null
                    ? aggregator.retrieveAlbum(mCurrentSong.getAlbum(), mCurrentSong.getProvider())
                    : null;

            // Song title
            mBaseTemplate.setTextViewText(R.id.tvNotifLineOne, mCurrentSong.getTitle());
            mExpandedTemplate.setTextViewText(R.id.tvNotifLineOne, mCurrentSong.getTitle());

            if (artist != null && artist.getName() != null && !artist.getName().isEmpty()) {
                // Artist name
                mBaseTemplate.setTextViewText(R.id.tvNotifLineTwo, artist.getName());
                mExpandedTemplate.setTextViewText(R.id.tvNotifLineTwo, artist.getName());
            } else {
                mBaseTemplate.setTextViewText(R.id.tvNotifLineTwo, null);
                mExpandedTemplate.setTextViewText(R.id.tvNotifLineTwo, null);
            }

            if (album != null && album.getName() != null && !album.getName().isEmpty()) {
                // Album name
                mExpandedTemplate.setTextViewText(R.id.tvNotifLineThree, album.getName());
            } else {
                mExpandedTemplate.setTextViewText(R.id.tvNotifLineThree, null);
            }
        } else {
            mBuilder.setContentTitle(getString(R.string.loading));
        }
    }

    if (mShowPlayAction) {
        mBaseTemplate.setImageViewResource(R.id.btnNotifPlayPause, R.drawable.ic_notif_play);
        mExpandedTemplate.setImageViewResource(R.id.btnNotifPlayPause, R.drawable.ic_notif_play);
    } else {
        mBaseTemplate.setImageViewResource(R.id.btnNotifPlayPause, R.drawable.ic_notif_pause);
        mExpandedTemplate.setImageViewResource(R.id.btnNotifPlayPause, R.drawable.ic_notif_pause);
    }

    if (mHasNext) {
        mBaseTemplate.setViewVisibility(R.id.btnNotifNext, View.VISIBLE);
        mExpandedTemplate.setViewVisibility(R.id.btnNotifNext, View.VISIBLE);
    } else {
        mBaseTemplate.setViewVisibility(R.id.btnNotifNext, View.GONE);
        mExpandedTemplate.setViewVisibility(R.id.btnNotifNext, View.GONE);
    }

    if (mCurrentArt != null && !mCurrentArt.getBitmap().isRecycled()) {
        mBaseTemplate.setImageViewBitmap(R.id.ivAlbumArt, mCurrentArt.getBitmap());
        mExpandedTemplate.setImageViewBitmap(R.id.ivAlbumArt, mCurrentArt.getBitmap());
    } else {

        mBaseTemplate.setImageViewResource(R.id.ivAlbumArt, R.drawable.album_placeholder);
        mExpandedTemplate.setImageViewResource(R.id.ivAlbumArt, R.drawable.album_placeholder);
    }

    // Post update
    if (mListener != null) {
        mListener.onNotificationChanged(this);
    }
}

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

public static void batteryLowAlert(final Context context) {
    if (sNotificationManager == null) {
        sNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }//  w ww. ja  va2s .c  om

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_alert_circle_white_24dp).setContentTitle("Battery is low")
            .setContentText("Connect your phone to a power source").setAutoCancel(true).setOngoing(false)
            .setLights(Color.RED, 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);
    }

    // Because the ID remains unchanged, the existing notification is updated.
    sNotificationManager.notify(Config.NOTIFICATION_BATTERY_LOW, mBuilder.build());
}

From source file:de.qspool.clementineremote.backend.downloader.DownloadManager.java

public boolean addJob(ClementineMessage clementineMessage) {
    ClementineSongDownloader songDownloader = new ClementineSongDownloader();
    songDownloader.setId(mIds);/*from w w  w  .  ja  v a2s.c o m*/
    songDownloader.setSongDownloaderListener(new SongDownloaderListener() {

        @Override
        public void onProgress(DownloadStatus progress) {
            DownloadManager.this.onProgress(progress);
        }

        @SuppressLint("InlinedApi")
        @Override
        public void onDownloadResult(DownloaderResult result) {
            int id = result.getId();

            // Move the download to the finished list
            mFinishedDownloads.append(id, mActiveDownloads.get(id));
            mActiveDownloads.remove(id);

            // Remove the notification if this was the last download
            if (mActiveDownloads.size() == 0) {
                mNotifyManager.cancel(NOTIFICATION_ID_DOWNLOADS);
            } else {
                // Build a new active notification as the old one might be
                // expanded and looks now strange
                createNewActiveNotification();
            }

            //download_noti_n_finished
            String title = mContext.getResources().getQuantityString(R.plurals.download_noti_n_finished,
                    mFinishedDownloads.size(), mFinishedDownloads.size());
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext)
                    .setContentTitle(title).setContentText(mContext.getString(result.getMessageStringId()))
                    .setSmallIcon(R.drawable.ic_launcher).setPriority(NotificationCompat.PRIORITY_MIN)
                    .setAutoCancel(true).setContentIntent(buildNotificationIntent())
                    .setVisibility(Notification.VISIBILITY_PUBLIC);

            // Displays the progress bar for the first time.
            mNotifyManager.notify(NOTIFICATION_ID_DOWNLOADS_FINISHED, notificationBuilder.build());
        }
    });

    mSharedPref = PreferenceManager.getDefaultSharedPreferences(mContext);

    // Get preferences and set download settings
    String defaultPath;
    if (mContext.getExternalFilesDir(Environment.DIRECTORY_MUSIC) == null
            && !mSharedPref.contains(SharedPreferencesKeys.SP_DOWNLOAD_DIR)) {
        Toast.makeText(mContext, R.string.download_noti_not_mounted, Toast.LENGTH_LONG).show();
        return false;
    } else {
        File defaultFile = mContext.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
        if (defaultFile != null)
            defaultPath = defaultFile.getAbsolutePath();
        else
            defaultPath = "";
    }

    songDownloader.setDownloadPath(mSharedPref.getString(SharedPreferencesKeys.SP_DOWNLOAD_DIR, defaultPath));
    songDownloader.setDownloadOnWifiOnly(mSharedPref.getBoolean(SharedPreferencesKeys.SP_WIFI_ONLY, false));
    songDownloader.setCreatePlaylistDir(
            mSharedPref.getBoolean(SharedPreferencesKeys.SP_DOWNLOAD_SAVE_OWN_DIR, false));
    songDownloader.setCreateArtistDir(
            mSharedPref.getBoolean(SharedPreferencesKeys.SP_DOWNLOAD_PLAYLIST_CRT_ARTIST_DIR, true));
    songDownloader.setCreateAlbumDir(
            mSharedPref.getBoolean(SharedPreferencesKeys.SP_DOWNLOAD_PLAYLIST_CRT_ALBUM_DIR, true));
    songDownloader.setOverrideExistingFiles(
            mSharedPref.getBoolean(SharedPreferencesKeys.SP_DOWNLOAD_OVERRIDE, false));

    // Show a toast that the download is starting
    Toast.makeText(mContext, R.string.player_download_started, Toast.LENGTH_SHORT).show();

    mActiveDownloads.append(mIds, songDownloader);

    mIds++;

    songDownloader.startDownload(clementineMessage);

    return true;
}

From source file: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  ava 2s  . c  o m

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_alert_circle_white_24dp).setContentTitle("Battery warning")
            .setContentText("Temperature is getting 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);
    }

    // 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.github.sryze.wirebug.DebugStatusService.java

private void updateStatus() {
    Log.i(TAG, "Performing a status update...");

    boolean isEnabled = DebugManager.isTcpDebuggingEnabled();
    if (isEnabled != isCurrentlyEnabled) {
        Log.i(TAG, String.format("Status has changed to %s", isEnabled ? "enabled" : "disabled"));
        sendStatusChangedBroadcast(isEnabled);
    } else {//w  w w  .  jav a2  s. co  m
        Log.i(TAG, "Status is unchanged");
    }

    if (keyguardManager.inKeyguardRestrictedInputMode() && preferences.getBoolean("disable_on_lock", false)) {
        Log.i(TAG, "Disabling debugging because disable_on_lock is true");
        DebugManager.setTcpDebuggingEnabled(false);
    }

    if (isEnabled) {
        boolean isConnectedToWifi = NetworkUtils.isConnectedToWifi(connectivityManager);
        Log.d(TAG, String.format("Connected to Wi-Fi: %s", isConnectedToWifi ? "yes" : "no"));

        Intent contentIntent = new Intent(this, MainActivity.class);
        contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        Intent stopIntent = new Intent(this, DebugStatusService.class);
        stopIntent.setAction(ACTION_STOP);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(getString(R.string.notification_title))
                .setContentIntent(
                        PendingIntent.getActivity(this, 0, contentIntent, PendingIntent.FLAG_CANCEL_CURRENT))
                .addAction(R.drawable.ic_stop, getString(R.string.notification_action_stop),
                        PendingIntent.getService(this, 0, stopIntent, PendingIntent.FLAG_CANCEL_CURRENT));

        if (isConnectedToWifi) {
            notificationBuilder.setContentText(String.format(getString(R.string.notification_text),
                    NetworkUtils.getWifiIpAddressString(wifiManager),
                    NetworkUtils.getWifiNetworkName(wifiManager)));
        } else {
            notificationBuilder.setContentText(getString(R.string.notification_text_not_connected));
        }

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            notificationBuilder.setCategory(Notification.CATEGORY_STATUS)
                    .setVisibility(Notification.VISIBILITY_PUBLIC);
        }

        Notification notification = notificationBuilder.build();
        notification.flags |= Notification.FLAG_NO_CLEAR;
        notificationManager.notify(STATUS_NOTIFICATION_ID, notification);
    } else {
        Log.d(TAG, "Canceling the notification");
        notificationManager.cancel(STATUS_NOTIFICATION_ID);
    }

    if (isEnabled && preferences.getBoolean("stay_awake", false)) {
        if (wakeLock != null && !wakeLock.isHeld()) {
            Log.i(TAG, "Acquiring a wake lock because stay_awake is true");
            wakeLock.acquire();
        }
    } else {
        if (wakeLock != null && wakeLock.isHeld()) {
            Log.i(TAG, "Releasing the wake lock");
            wakeLock.release();
        }
    }

    isCurrentlyEnabled = isEnabled;
}

From source file:com.giovanniterlingen.windesheim.controllers.NotificationController.java

public void initNotificationChannels() {
    if (android.os.Build.VERSION.SDK_INT >= 26) {
        NotificationChannel pushChannel = new NotificationChannel(PUSH_NOTIFICATION_CHANNEL,
                ApplicationLoader.applicationContext.getResources().getString(R.string.push_notification),
                NotificationManager.IMPORTANCE_HIGH);

        pushChannel.setDescription(ApplicationLoader.applicationContext.getResources()
                .getString(R.string.push_notification_description));
        pushChannel.enableLights(true);//  ww  w .java  2 s. c  o  m
        pushChannel.enableVibration(true);
        pushChannel.setShowBadge(true);
        pushChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        NotificationChannel persistentChannel = new NotificationChannel(PERSISTENT_NOTIFICATION_CHANNEL,
                ApplicationLoader.applicationContext.getResources().getString(R.string.persistent_notification),
                NotificationManager.IMPORTANCE_MIN);

        persistentChannel.setDescription(ApplicationLoader.applicationContext.getResources()
                .getString(R.string.persistent_notification_description));
        persistentChannel.enableLights(false);
        persistentChannel.enableVibration(false);
        persistentChannel.setShowBadge(false);
        persistentChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        NotificationChannel serviceChannel = new NotificationChannel(SERVICE_NOTIFICATION_CHANNEL,
                ApplicationLoader.applicationContext.getResources().getString(R.string.service_notification),
                NotificationManager.IMPORTANCE_MIN);

        serviceChannel.setDescription(ApplicationLoader.applicationContext.getResources()
                .getString(R.string.service_notification_description));
        serviceChannel.enableLights(false);
        serviceChannel.enableVibration(false);
        serviceChannel.setShowBadge(false);
        serviceChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);

        NotificationManager mManager = (NotificationManager) ApplicationLoader.applicationContext
                .getSystemService(Context.NOTIFICATION_SERVICE);

        mManager.createNotificationChannel(pushChannel);
        mManager.createNotificationChannel(persistentChannel);
        mManager.createNotificationChannel(serviceChannel);
    }
}

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

public static void batteryFullAlert(final Context context) {
    if (sNotificationManager == null) {
        sNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }//from w ww  .  j a  va2  s  . c  om

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_information_white_24dp)
            .setContentTitle(context.getString(R.string.notif_battery_full))
            .setContentText(context.getString(R.string.notif_remove_charger)).setAutoCancel(true)
            .setOngoing(false).setLights(Color.GREEN, 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);

    // 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.
    sNotificationManager.notify(Config.NOTIFICATION_BATTERY_FULL, mBuilder.build());
}

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

@Override
public Notification build(Context context, NotificationModel nm, List<Timer> unexpired) {
    final Timer timer = unexpired.get(0);
    final int count = unexpired.size();

    // Compute some values required below.
    final boolean running = timer.isRunning();
    final Resources res = context.getResources();

    final long base = getChronometerBase(timer);
    final String pname = context.getPackageName();
    final RemoteViews content = new RemoteViews(pname, R.layout.chronometer_notif_content);
    content.setChronometerCountDown(R.id.chronometer, true);
    content.setChronometer(R.id.chronometer, base, null, running);

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

    final CharSequence stateText;
    if (count == 1) {
        if (running) {
            // Single timer is running.
            if (TextUtils.isEmpty(timer.getLabel())) {
                stateText = res.getString(R.string.timer_notification_label);
            } else {
                stateText = timer.getLabel();
            }//from  w ww .j  ava 2 s  .  c  o  m

            // Left button: Pause
            final Intent pause = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

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

            // Right Button: +1 Minute
            final Intent addMinute = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_ADD_MINUTE_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

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

        } else {
            // Single timer is paused.
            stateText = res.getString(R.string.timer_paused);

            // Left button: Start
            final Intent start = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_START_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

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

            // Right Button: Reset
            final Intent reset = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_RESET_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

            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());
        }
    } else {
        if (running) {
            // At least one timer is running.
            stateText = res.getString(R.string.timers_in_use, count);
        } else {
            // All timers are paused.
            stateText = res.getString(R.string.timers_stopped, count);
        }

        final Intent reset = TimerService.createResetUnexpiredTimersIntent(context);

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

    content.setTextViewText(R.id.state, stateText);

    // Intent to load the app and show the timer when the notification is tapped.
    final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_TIMERS)
            .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId())
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, R.string.label_notification);

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

    return new Notification.Builder(context).setOngoing(true).setLocalOnly(true).setShowWhen(false)
            .setAutoCancel(false).setCustomContentView(content).setContentIntent(pendingShowApp)
            .setPriority(Notification.PRIORITY_HIGH).setCategory(Notification.CATEGORY_ALARM)
            .setSmallIcon(R.drawable.stat_notify_timer).setGroup(nm.getTimerNotificationGroupKey())
            .setVisibility(Notification.VISIBILITY_PUBLIC).setStyle(new Notification.DecoratedCustomViewStyle())
            .setActions(actions.toArray(new Notification.Action[actions.size()]))
            .setColor(ContextCompat.getColor(context, R.color.default_background)).build();
}