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.teinproductions.tein.papyrosprogress.UpdateCheckReceiver.java

private static void issueNotification(Context context, Set<String> addedMilestones,
        Set<String> removedMilestones, Map<String, int[]> changedProgresses) {
    String title = context.getString(R.string.notification_title);
    StringBuilder message = new StringBuilder();

    // Changed progresses
    for (String milestoneTitle : changedProgresses.keySet()) {
        int oldProgress = changedProgresses.get(milestoneTitle)[0];
        int newProgress = changedProgresses.get(milestoneTitle)[1];
        message.append("\n").append(String.format(context.getString(R.string.notific_msg_text_format),
                milestoneTitle, oldProgress, newProgress));
    }//w w  w  . java 2 s.c  o m

    // Added milestones
    for (String milestoneTitle : addedMilestones) {
        message.append("\n").append(context.getString(R.string.milestone_added_notification, milestoneTitle));
    }

    // Removed milestones
    for (String milestoneTitle : removedMilestones) {
        message.append("\n").append(context.getString(R.string.milestone_removed_notification, milestoneTitle));
    }

    // Remove first newline
    message.delete(0, 1);

    // Create PendingIntent
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class),
            PendingIntent.FLAG_UPDATE_CURRENT);

    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
    boolean sound = pref.getBoolean(Constants.NOTIFICATION_SOUND_PREF, true);
    boolean vibrate = pref.getBoolean(Constants.NOTIFICATION_VIBRATE_PREF, true);
    boolean light = pref.getBoolean(Constants.NOTIFICATION_LIGHT_PREF, true);
    int defaults = 0;
    if (sound)
        defaults = defaults | Notification.DEFAULT_SOUND;
    if (vibrate)
        defaults = defaults | Notification.DEFAULT_VIBRATE;
    if (light)
        defaults = defaults | Notification.DEFAULT_LIGHTS;

    // Build the notification
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContentTitle(title)
            .setContentText(message).setContentIntent(pendingIntent)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setSmallIcon(R.mipmap.notification_small_icon)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
            .setDefaults(defaults).setAutoCancel(true);

    // Issue the notification
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(PROGRESS_NOTIFICATION_ID, builder.build());
}

From source file:cat.wuyingren.whatsannoy.utils.SystemUtils.java

public static int createScheduleNotification(Context context, Schedule s) {
    Log.w("UTILS", "createScheduleNotification()");
    ScheduleDataSource dataSource;/*from   w w  w .  ja  v  a 2s . co m*/

    dataSource = new ScheduleDataSource(context);
    dataSource.open();
    //  Random r = new Random();
    int mId = 0;// r.nextInt();
    //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String uri = s.getRingtone(); // prefs.getString("pref_general_sound_key", "");
    Uri ringtone = Uri.parse(uri);
    if (uri == "") { // isEmpty() is not available on API 7
        ringtone = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION);
    }
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(context.getResources().getString(R.string.app_name))
            .setContentText(context.getResources().getString(R.string.notification));
    // 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(MainActivity.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);
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mBuilder.setSound(ringtone);
    //mBuilder.setWhen(s.getDate());
    mNotificationManager.notify(mId, mBuilder.build());
    s.setIsEnabled(false);
    dataSource.updateSchedule(context, s);
    dataSource.close();
    return mId;
}

From source file:Main.java

public static void notifyShort(@NonNull Context context, @NonNull String title, String msg,
        @DrawableRes int iconId, @NonNull NotificationCompat.Action action, int nId) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new NotificationCompat.Builder(context).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL).setContentTitle(title).setContentText(msg)
            .setSmallIcon(iconId).addAction(action).setContentIntent(action.actionIntent).build();
    notificationManager.notify(nId, notification);
}

From source file:com.cyanogenmod.account.util.CMAccountUtils.java

public static void showNotification(Context context, int id, Notification notification) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(id, notification);
}

From source file:Main.java

public static void notifyBig(@NonNull Context context, @NonNull String title, @NonNull String msg,
        @DrawableRes int iconId) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new NotificationCompat.Builder(context).setAutoCancel(true)
            .setContentTitle(title).setDefaults(Notification.DEFAULT_ALL).setContentText(msg)
            .setSmallIcon(iconId).setStyle(new NotificationCompat.BigTextStyle().setBigContentTitle(title)
                    .setSummaryText(msg).bigText(msg))
            .build();//  www .j  a v  a  2 s.com
    notificationManager.notify(NOTIFICATION_ID, notification);
}

From source file:com.battlelancer.seriesguide.billing.BillingActivity.java

/**
 * Displays a notification that the subscription has expired. Its action opens {@link
 * BillingActivity}./*from w w w . ja  v a 2 s. c  o  m*/
 */
public static void onExpiredNotification(Context context) {
    NotificationCompat.Builder nb = new NotificationCompat.Builder(context);

    // set required attributes
    nb.setSmallIcon(R.drawable.ic_notification);
    nb.setContentTitle(context.getString(R.string.subscription_expired));
    nb.setContentText(context.getString(R.string.subscription_expired_details));

    // set additional attributes
    nb.setDefaults(Notification.DEFAULT_LIGHTS);
    nb.setAutoCancel(true);
    nb.setTicker(context.getString(R.string.subscription_expired_details));
    nb.setPriority(NotificationCompat.PRIORITY_DEFAULT);

    // build task stack
    Intent notificationIntent = new Intent(context, BillingActivity.class);
    PendingIntent contentIntent = TaskStackBuilder.create(context)
            .addNextIntent(new Intent(context, ShowsActivity.class))
            .addNextIntent(new Intent(context, SeriesGuidePreferences.class)).addNextIntent(notificationIntent)
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    nb.setContentIntent(contentIntent);

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

    // show the notification
    final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(R.string.subscription_expired, notification);
}

From source file:Main.java

public static void notifyWithImage(@NonNull Context context, @NonNull String title, @NonNull String msg,
        @DrawableRes int iconId, @NonNull Bitmap bitmap) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new NotificationCompat.Builder(context).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL).setContentTitle(title).setContentText(msg)
            .setSmallIcon(iconId).setStyle(new NotificationCompat.BigPictureStyle().setBigContentTitle(title)
                    .setSummaryText(msg).bigPicture(bitmap))
            .build();//from   w w  w .  ja  v  a  2 s.c  o  m
    notificationManager.notify(NOTIFICATION_ID, notification);
}

From source file:com.chintans.venturebox.util.Utils.java

public static void showNotification(Context context, PackageInfo[] infosRom, PackageInfo[] infosGapps) {
    Resources resources = context.getResources();

    if (infosRom != null) {
        sPackageInfosRom = infosRom;/*from  www.  j  av a 2  s  . c  o  m*/
    } else {
        infosRom = sPackageInfosRom;
    }
    if (infosGapps != null) {
        sPackageInfosGapps = infosGapps;
    } else {
        infosGapps = sPackageInfosGapps;
    }

    Intent intent = new Intent(context, MainActivity.class);
    NotificationInfo fileInfo = new NotificationInfo();
    fileInfo.mNotificationId = Updater.NOTIFICATION_ID;
    fileInfo.mPackageInfosRom = infosRom;
    fileInfo.mPackageInfosGapps = infosGapps;
    intent.putExtra(FILES_INFO, fileInfo);
    PendingIntent pIntent = PendingIntent.getActivity(context, Updater.NOTIFICATION_ID, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentTitle(resources.getString(R.string.new_system_update))
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.ic_launcher))
            .setContentIntent(pIntent);

    String contextText = "";
    if (infosRom.length + infosGapps.length == 1) {
        String filename = infosRom.length == 1 ? infosRom[0].getFilename() : infosGapps[0].getFilename();
        contextText = resources.getString(R.string.new_package_name, new Object[] { filename });
    } else {
        contextText = resources.getString(R.string.new_packages,
                new Object[] { infosRom.length + infosGapps.length });
    }
    builder.setContentText(contextText);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle(context.getResources().getString(R.string.new_system_update));
    if (infosRom.length + infosGapps.length > 1) {
        inboxStyle.addLine(contextText);
    }
    for (int i = 0; i < infosRom.length; i++) {
        inboxStyle.addLine(infosRom[i].getFilename());
    }
    for (int i = 0; i < infosGapps.length; i++) {
        inboxStyle.addLine(infosGapps[i].getFilename());
    }
    inboxStyle.setSummaryText(resources.getString(R.string.app_name));
    builder.setStyle(inboxStyle);

    Notification notif = builder.build();

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Service.NOTIFICATION_SERVICE);

    notif.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(Updater.NOTIFICATION_ID, notif);
}

From source file:com.afwsamples.testdpc.common.Util.java

public static void showNotification(Context context, int titleId, String msg, int notificationId) {
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification.Builder(context).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(context.getString(titleId)).setContentText(msg)
            .setStyle(new Notification.BigTextStyle().bigText(msg)).build();
    mNotificationManager.notify(notificationId, notification);
}

From source file:com.matze5800.paupdater.Functions.java

public static void Notify(Context context, String text) {
    Log.i("notify", text);
    Notification.Builder nBuilder;
    nBuilder = new Notification.Builder(context);
    Intent resultIntent = new Intent(context, Cancel.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(Cancel.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    nBuilder.setContentIntent(resultPendingIntent);
    nBuilder.setSmallIcon(R.drawable.ic_launcher);
    nBuilder.setContentTitle("PA Updater");
    nBuilder.setContentText(text);/*from   w  w  w. j a v  a  2  s  . com*/
    nBuilder.setAutoCancel(false);
    nBuilder.setOngoing(true);
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, nBuilder.build());
}