Example usage for android.app NotificationManager notify

List of usage examples for android.app NotificationManager notify

Introduction

In this page you can find the example usage for android.app NotificationManager notify.

Prototype

public void notify(int id, Notification notification) 

Source Link

Document

Post a notification to be shown in the status bar.

Usage

From source file:com.commonsware.android.ordered.NoticeReceiver.java

@Override
public void onReceive(Context ctxt, Intent intent) {
    NotificationManager mgr = (NotificationManager) ctxt.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder b = new NotificationCompat.Builder(ctxt);
    PendingIntent pi = PendingIntent.getActivity(ctxt, 0, new Intent(ctxt, OrderedActivity.class), 0);

    b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis())
            .setContentTitle(ctxt.getString(R.string.notify_title))
            .setContentText(ctxt.getString(R.string.notify_text))
            .setSmallIcon(android.R.drawable.stat_notify_chat).setTicker(ctxt.getString(R.string.notify_ticker))
            .setContentIntent(pi);//  ww w .  j  a  v  a2  s  .  c om

    mgr.notify(NOTIFY_ME_ID, b.build());
}

From source file:com.a3did.partner.recosample.RecoBackgroundRangingService.java

private void popupNotification(String msg) {
    Log.i("BackRangingService", "popupNotification()");
    String currentTime = new SimpleDateFormat("HH:mm:ss", Locale.KOREA).format(new Date());
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle(msg + " " + currentTime).setContentText(msg);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    builder.setStyle(inboxStyle);/* w w w  . ja  v a 2  s  .c o  m*/
    nm.notify(mNotificationID, builder.build());
    mNotificationID = (mNotificationID - 1) % 1000 + 9000;
}

From source file:com.ds.owl.dsdormitory.RecoBackgroundRangingService.java

private void popupNotification(String msg) {
    Log.i("BackRangingService", "popupNotification()");
    String currentTime = new SimpleDateFormat("HH:mm:ss", Locale.KOREA).format(new Date());
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle(msg + " " + currentTime).setContentText(msg);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    builder.setStyle(inboxStyle);//w  w  w.  j  a v  a  2s. c om
    nm.notify(mNotificationID, builder.build());
    mNotificationID = (mNotificationID - 1) % 1000 + 9000;

}

From source file:jieehd.villain.updater.checkInBackground.java

public void notifyUser() {
    final NotificationManager nm;
    nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence from = "Villain Updater";
    CharSequence message = "New Updates!";
    PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0);
    Notification notif = new Notification(R.drawable.updates, "New updates!", System.currentTimeMillis());
    notif.setLatestEventInfo(mContext, from, message, contentIntent);
    nm.notify(1, notif);
}

From source file:com.battlelancer.seriesguide.service.NotificationService.java

private void onNotify(final Cursor upcomingEpisodes, List<Integer> notifyPositions, long latestAirtime) {
    final Context context = getApplicationContext();

    CharSequence tickerText;/*from   w  w  w .ja  v  a 2 s.c  o m*/
    CharSequence contentTitle;
    CharSequence contentText;
    PendingIntent contentIntent;
    // base intent for task stack
    final Intent showsIntent = new Intent(context, ShowsActivity.class);
    showsIntent.putExtra(ShowsActivity.InitBundle.SELECTED_TAB, ShowsActivity.InitBundle.INDEX_TAB_UPCOMING);

    final int count = notifyPositions.size();
    if (count == 1) {
        // notify in detail about one episode
        Timber.d("Notifying about 1 new episode");
        upcomingEpisodes.moveToPosition(notifyPositions.get(0));

        final String showTitle = upcomingEpisodes.getString(NotificationQuery.SHOW_TITLE);
        tickerText = getString(R.string.upcoming_show, showTitle);
        contentTitle = showTitle + " "
                + Utils.getEpisodeNumber(this, upcomingEpisodes.getInt(NotificationQuery.SEASON),
                        upcomingEpisodes.getInt(NotificationQuery.NUMBER));

        // "8:00 PM on Network"
        final String releaseTime = TimeTools.formatToLocalReleaseTime(this, TimeTools.getEpisodeReleaseTime(
                this, upcomingEpisodes.getLong(NotificationQuery.EPISODE_FIRST_RELEASE_MS)));
        final String network = upcomingEpisodes.getString(NotificationQuery.NETWORK);
        contentText = getString(R.string.upcoming_show_detailed, releaseTime, network);

        Intent episodeDetailsIntent = new Intent(context, EpisodesActivity.class);
        episodeDetailsIntent.putExtra(EpisodesActivity.InitBundle.EPISODE_TVDBID,
                upcomingEpisodes.getInt(NotificationQuery._ID));
        episodeDetailsIntent.putExtra(KEY_EPISODE_CLEARED_TIME, latestAirtime);

        contentIntent = TaskStackBuilder.create(context).addNextIntent(showsIntent)
                .addNextIntent(episodeDetailsIntent)
                .getPendingIntent(REQUEST_CODE_SINGLE_EPISODE, PendingIntent.FLAG_CANCEL_CURRENT);
    } else {
        // notify about multiple episodes
        Timber.d("Notifying about " + count + " new episodes");
        tickerText = getString(R.string.upcoming_episodes);
        contentTitle = getString(R.string.upcoming_episodes_number, count);
        contentText = getString(R.string.upcoming_display);

        contentIntent = TaskStackBuilder.create(context)
                .addNextIntent(showsIntent.putExtra(KEY_EPISODE_CLEARED_TIME, latestAirtime))
                .getPendingIntent(REQUEST_CODE_MULTIPLE_EPISODES, PendingIntent.FLAG_CANCEL_CURRENT);
    }

    final NotificationCompat.Builder nb = new NotificationCompat.Builder(context);

    if (AndroidUtils.isJellyBeanOrHigher()) {
        Timber.d("Building rich notification (JB+)");
        // JELLY BEAN and above
        if (count == 1) {
            // single episode
            upcomingEpisodes.moveToPosition(notifyPositions.get(0));
            maybeSetPoster(context, nb, upcomingEpisodes.getString(NotificationQuery.POSTER));

            final String episodeTitle = upcomingEpisodes.getString(NotificationQuery.TITLE);
            final String episodeSummary = upcomingEpisodes.getString(NotificationQuery.OVERVIEW);

            final SpannableStringBuilder bigText = new SpannableStringBuilder();
            bigText.append(TextUtils.isEmpty(episodeTitle) ? "" : episodeTitle);
            bigText.setSpan(new StyleSpan(Typeface.BOLD), 0, bigText.length(), 0);
            bigText.append("\n");
            bigText.append(TextUtils.isEmpty(episodeSummary) ? "" : episodeSummary);

            nb.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText).setSummaryText(contentText));

            // Action button to check in
            Intent checkInActionIntent = new Intent(context, QuickCheckInActivity.class);
            checkInActionIntent.putExtra(QuickCheckInActivity.InitBundle.EPISODE_TVDBID,
                    upcomingEpisodes.getInt(NotificationQuery._ID));
            checkInActionIntent
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            PendingIntent checkInIntent = PendingIntent.getActivity(context, REQUEST_CODE_ACTION_CHECKIN,
                    checkInActionIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            nb.addAction(R.drawable.ic_action_checkin, getString(R.string.checkin), checkInIntent);
        } else {
            // multiple episodes
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

            // display at most the first five
            for (int displayIndex = 0; displayIndex < Math.min(count, 5); displayIndex++) {
                if (!upcomingEpisodes.moveToPosition(notifyPositions.get(displayIndex))) {
                    // could not go to the desired position (testing just in case)
                    break;
                }

                final SpannableStringBuilder lineText = new SpannableStringBuilder();

                // show title
                String showTitle = upcomingEpisodes.getString(NotificationQuery.SHOW_TITLE);
                lineText.append(TextUtils.isEmpty(showTitle) ? "" : showTitle);
                lineText.setSpan(new StyleSpan(Typeface.BOLD), 0, lineText.length(), 0);

                lineText.append(" ");

                // "8:00 PM on Network"
                String releaseTime = TimeTools.formatToLocalReleaseTime(this, TimeTools.getEpisodeReleaseTime(
                        this, upcomingEpisodes.getLong(NotificationQuery.EPISODE_FIRST_RELEASE_MS)));
                String network = upcomingEpisodes.getString(NotificationQuery.NETWORK);
                lineText.append(getString(R.string.upcoming_show_detailed, releaseTime, network));

                inboxStyle.addLine(lineText);
            }

            // tell if we could not display all episodes
            if (count > 5) {
                inboxStyle.setSummaryText(getString(R.string.more, count - 5));
            }

            nb.setStyle(inboxStyle);
            nb.setContentInfo(String.valueOf(count));
        }
    } else {
        // ICS and below
        if (count == 1) {
            // single episode
            upcomingEpisodes.moveToPosition(notifyPositions.get(0));
            maybeSetPoster(context, nb, upcomingEpisodes.getString(NotificationQuery.POSTER));
        }
    }

    // notification sound
    final String ringtoneUri = NotificationSettings.getNotificationsRingtone(context);
    // If the string is empty, the user chose silent...
    if (ringtoneUri.length() != 0) {
        // ...otherwise set the specified ringtone
        Timber.d("Notification has sound");
        nb.setSound(Uri.parse(ringtoneUri));
    }
    // vibration
    if (NotificationSettings.isNotificationVibrating(context)) {
        Timber.d("Notification vibrates");
        nb.setVibrate(VIBRATION_PATTERN);
    }
    nb.setDefaults(Notification.DEFAULT_LIGHTS);
    nb.setWhen(System.currentTimeMillis());
    nb.setAutoCancel(true);
    nb.setTicker(tickerText);
    nb.setContentTitle(contentTitle);
    nb.setContentText(contentText);
    nb.setContentIntent(contentIntent);
    nb.setSmallIcon(R.drawable.ic_notification);
    nb.setColor(getResources().getColor(R.color.accent_primary));
    nb.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    nb.setCategory(NotificationCompat.CATEGORY_EVENT);

    Timber.d("Setting delete intent with episode time: " + latestAirtime);
    Intent i = new Intent(this, NotificationService.class);
    i.putExtra(KEY_EPISODE_CLEARED_TIME, latestAirtime);
    PendingIntent deleteIntent = PendingIntent.getService(this, REQUEST_CODE_DELETE_INTENT, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
    nb.setDeleteIntent(deleteIntent);

    // build the notification
    Notification notification = nb.build();

    // use string resource id, always unique within app
    final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(R.string.upcoming_show, notification);
}

From source file:com.baruckis.nanodegree.spotifystreamer.PlayerService.java

private void showCancelNotification(boolean visible) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    if (mNotification == null)
        return;/*w  w  w .ja  v a  2s  . c o  m*/
    if (visible) {
        notificationManager.notify(1, mNotification);
    } else {
        notificationManager.cancel(1);
    }
}

From source file:com.actinarium.rhythm.control.RhythmNotificationService.java

private void handleShowNotification(int notificationId) {
    Application application = getApplication();
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notification;// w  ww.  ja v  a  2s .  c  om

    // If the application is not the host - show error and return.
    if (!(application instanceof RhythmControl.Host)) {
        notification = makeCommonNotification(getString(R.string.arl_no_host_text))
                .setColor(NOTIFICATION_ERROR_COLOR).setContentTitle(getString(R.string.arl_no_host_title))
                .build();
        manager.notify(notificationId, notification);
        return;
    }

    RhythmControl control = ((RhythmControl.Host) application).getRhythmControl();
    RhythmGroup currentGroup = control.getCurrentNotificationGroup();

    // If there are no groups yet - show warning and return.
    if (currentGroup == null) {
        notification = makeCommonNotification(getString(R.string.arl_no_groups_text))
                .setColor(NOTIFICATION_ERROR_COLOR).setContentTitle(getString(R.string.arl_no_groups_title))
                .build();
        manager.notify(notificationId, notification);
        return;
    }

    // Now if everything is OK:
    Intent nextGroupAction = new Intent(this, RhythmNotificationService.class);
    nextGroupAction.setAction(ACTION_NEXT_GROUP);
    PendingIntent piNextGroupAction = PendingIntent.getService(this, 0, nextGroupAction,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Intent nextOverlayAction = new Intent(this, RhythmNotificationService.class);
    nextOverlayAction.setAction(ACTION_NEXT_OVERLAY);
    PendingIntent piNextOverlayAction = PendingIntent.getService(this, 0, nextOverlayAction,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Intent dismissAction = new Intent(this, RhythmNotificationService.class);
    dismissAction.setAction(ACTION_DISMISS_QUICK_CONTROL);
    PendingIntent piDismissAction = PendingIntent.getService(this, 0, dismissAction,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // todo: another action when notification is clicked (control activity will be added in v1.0)

    // Determine what to write in notification
    RhythmOverlay currentOverlay = currentGroup.getCurrentOverlay();
    String groupText = getString(R.string.arl_group, currentGroup.toString());
    String overlayText = currentOverlay == null ? getString(R.string.arl_no_overlay)
            : getString(R.string.arl_overlay, currentOverlay.toString());

    // Finally, build and display the notification
    notification = makeCommonNotification(overlayText).setColor(NOTIFICATION_ICON_COLOR)
            .setContentTitle(groupText).setDeleteIntent(piDismissAction)
            .addAction(new NotificationCompat.Action(R.drawable.arl_loop, getString(R.string.arl_next_group),
                    piNextGroupAction))
            .addAction(new NotificationCompat.Action(R.drawable.arl_loop, getString(R.string.arl_next_overlay),
                    piNextOverlayAction))
            .build();
    manager.notify(notificationId, notification);
}

From source file:com.cloudbees.gasp.gcm.GCMIntentService.java

/**
 * Send a notification message that a new update has been received
 *
 * @param msg Notification message//ww  w  .j a v a  2s  .  co m
 */
private void sendNotification(String msg) {
    NotificationManager mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ConsoleActivity.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_gcm).setContentTitle("Gasp! Update")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

From source file:ca.appvelopers.mcgillmobile.util.background.CheckerService.java

/**
 * Generates a local notification which will redirect the user to the right portion of the app
 *  when clicked/*from   w w w .  j a va 2s .c  o m*/
 *
 * @param intent  The intent to use when clicked
 * @param message The message to display
 * @param id      The notification Id (to update any existing ones)
 */
private void createNotification(Intent intent, String message, int id) {
    //Get the notification manager
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message)).setContentText(message)
            .setContentIntent(pendingIntent);

    manager.notify(id, builder.build());
}