Example usage for android.content Context NOTIFICATION_SERVICE

List of usage examples for android.content Context NOTIFICATION_SERVICE

Introduction

In this page you can find the example usage for android.content Context NOTIFICATION_SERVICE.

Prototype

String NOTIFICATION_SERVICE

To view the source code for android.content Context NOTIFICATION_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.NotificationManager for informing the user of background events.

Usage

From source file:com.ithinkbest.taipeiok.NavigationDrawerActivity.java

private void notifyAppWebpage() {
    int idGooglePlay = 12346;
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.to_app_webpage));
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ToAppWebpageActivity.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(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ToAppWebpageActivity.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) getSystemService(
            Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(idGooglePlay, mBuilder.build());

}

From source file:com.chasetech.pcount.autoupdate.AutoUpdateApk.java

protected void raise_notification() {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager) context.getSystemService(ns);

    String update_file = preferences.getString(UPDATE_FILE, "");

    if (update_file.length() > 0) {
        setChanged();/*  w  w w .  ja v a  2 s.c om*/
        notifyObservers(AUTOUPDATE_HAVE_UPDATE);

        // raise notification
        //            Notification notification = new Notification(
        //                    appIcon, appName + " update", System.currentTimeMillis());
        //            notification.flags |= NOTIFICATION_FLAGS;

        CharSequence contentTitle = appName + " update available";
        CharSequence contentText = "Click this to install..";
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setDataAndType(
                Uri.parse("file://" + context.getFilesDir().getAbsolutePath() + "/" + update_file),
                ANDROID_PACKAGE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        Notification.Builder builder = new Notification.Builder(context);

        builder.setAutoCancel(false);
        builder.setTicker("An update is available.");
        builder.setContentTitle(contentTitle);
        builder.setContentText(contentText);
        builder.setSmallIcon(appIcon);
        builder.setContentIntent(contentIntent);
        builder.setOngoing(true);
        builder.setSubText(appName + " update"); //API level 16
        builder.setNumber(100);
        builder.build();

        Notification myNotication = builder.getNotification();
        nm.notify(NOTIFICATION_ID, myNotication);

        //            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        //            nm.notify( NOTIFICATION_ID, notification);
    } else {
        nm.cancel(NOTIFICATION_ID);
    }

    //      if( update_file.length() > 0 ) {
    //         setChanged();
    //         notifyObservers(AUTOUPDATE_HAVE_UPDATE);
    //
    //         // raise notification
    ///*         Notification notification = new Notification(
    //               appIcon, appName + " update", System.currentTimeMillis());*/
    //            Notification.Builder notifBuilder = new Notification.Builder(context);
    //         //notification.flags |= NOTIFICATION_FLAGS;
    //
    //         CharSequence contentTitle = appName + " update available";
    //         CharSequence contentText = "Select to install";
    //         Intent notificationIntent = new Intent(Intent.ACTION_VIEW );
    //         Uri uriFile = Uri.parse("file://" + context.getFilesDir().getAbsolutePath() + "/" + update_file);
    //         notificationIntent.setDataAndType(uriFile, ANDROID_PACKAGE);
    //         PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    //
    //            notifBuilder.setContentTitle(contentTitle);
    //            notifBuilder.setContentText(contentText);
    //            notifBuilder.setContentIntent(contentIntent);
    //            notifBuilder.setSubText("Update available.");
    //            notifBuilder.build();
    //
    //            Notification notification = notifBuilder.build();
    //            notification.flags |= NOTIFICATION_FLAGS;
    //
    //            nm.notify(NOTIFICATION_ID, notification);
    //
    //         //notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    //         //nm.notify( NOTIFICATION_ID, notification);
    //      } else {
    //         nm.cancel( NOTIFICATION_ID );
    //      }
}

From source file:com.andrewshu.android.reddit.common.Common.java

public static void newMailNotification(Context context, String mailNotificationStyle, int count) {
    Intent nIntent = new Intent(context, InboxActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, nIntent, 0);
    Notification notification = new Notification(R.drawable.mail, Constants.HAVE_MAIL_TICKER,
            System.currentTimeMillis());
    if (Constants.PREF_MAIL_NOTIFICATION_STYLE_BIG_ENVELOPE.equals(mailNotificationStyle)) {
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.big_envelope_notification);
        notification.contentView = contentView;
    } else {//  w  w  w  .  j  a  v a 2s  .c o  m
        notification.setLatestEventInfo(context, Constants.HAVE_MAIL_TITLE,
                count + (count == 1 ? " unread message" : " unread messages"), contentIntent);
    }
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
    notification.contentIntent = contentIntent;
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(Constants.NOTIFICATION_HAVE_MAIL, notification);
}

From source file:com.andrewshu.android.reddit.common.Common.java

public static void cancelMailNotification(Context context) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(Constants.NOTIFICATION_HAVE_MAIL);
}

From source file:com.smarthome.deskclock.Alarms.java

private static void clearSnoozePreference(final Context context, final SharedPreferences prefs) {
    final int alarmId = prefs.getInt(PREF_SNOOZE_ID, -1);
    if (alarmId != -1) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(alarmId);//from   ww  w . j ava  2  s.c  om
    }

    final SharedPreferences.Editor ed = prefs.edit();
    ed.remove(PREF_SNOOZE_ID);
    ed.remove(PREF_SNOOZE_TIME);
    ed.apply();
}

From source file:amigoinn.example.v4sales.AutoUpdateApk.java

protected void raise_notification() {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager) context.getSystemService(ns);

    String update_file = preferences.getString(UPDATE_FILE, "");
    if (update_file.length() > 0) {
        setChanged();/*from w  w  w  . j  av a  2 s  .  co  m*/
        notifyObservers(AUTOUPDATE_HAVE_UPDATE);

        // raise the notification
        CharSequence contentTitle = appName + " update available";
        CharSequence contentText = "Select to install";
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setDataAndType(
                Uri.parse("file://" + context.getFilesDir().getAbsolutePath() + "/" + update_file),
                ANDROID_PACKAGE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(appIcon);
        builder.setTicker(appName + " update");
        builder.setContentTitle(contentTitle);
        builder.setContentText(contentText);
        builder.setContentIntent(contentIntent);
        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(true);
        builder.setOngoing(true);

        nm.notify(NOTIFICATION_ID, builder.build());
    } else {
        //nm.cancel( NOTIFICATION_ID );   // tried this, but it just doesn't do the trick =(
        nm.cancelAll();
    }
}

From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncEngine.java

public static void notifyUser(final String msg, final int pct) {
    Intent notificationIntent = new Intent(context, FlowzrSyncActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    if (mNotifyBuilder == null) {
        mNotifyBuilder = new NotificationCompat.Builder(context).setAutoCancel(true)
                .setContentTitle(context.getString(R.string.app_name))
                .setContentText(context.getString(R.string.flowzr_sync_inprogress))
                .setSmallIcon(R.drawable.icon);

        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // Sets an ID for the notification, so it can be updated
    }/*from www.  ja va2s  .co m*/
    mNotifyBuilder.setContentText(msg);
    mNotifyBuilder.setContentIntent(pendingIntent);
    mNotifyBuilder.setAutoCancel(true).build();
    if (pct != 0) {
        mNotifyBuilder.setProgress(100, pct, false);
    }
    mNotificationManager.notify(NOTIFICATION_ID, mNotifyBuilder.build());
}

From source file:com.facebook.notifications.NotificationsManager.java

/**
 * Present a {@link Notification} to be presented from a GCM push bundle.
 * <p/>//from w w  w .  j a v a 2s .  c om
 * This does not present a notification immediately, instead it caches the assets from the
 * notification bundle, and then presents a notification to the user. This allows for a smoother
 * interaction without loading indicators for the user.
 * <p/>
 * Note that only one notification can be created for a specific push bundle, should you attempt
 * to present a new notification with the same payload bundle as an existing notification, it will
 * replace and update the old notification.
 *
 * @param context              The context to send the notification from
 * @param notificationBundle   The content of the push notification
 * @param launcherIntent       The launcher intent that contains your Application's activity.
 *                             This will be modified with the FLAG_ACTIVITY_CLEAR_TOP and
 *                             FLAG_ACTIVITY_SINGLE_TOP flags, in order to properly show the
 *                             notification in an already running application.
 *                             <p/>
 *                             Should you not want this behavior, you may use the notificationExtender
 *                             parameter to customize the contentIntent of the notification before
 *                             presenting it.
 * @param notificationExtender A nullable argument that allows you to customize the notification
 *                             before displaying it. Use this to configure Icons, text, sounds,
 *                             etc. before we pass the notification off to the OS.
 */
public static boolean presentNotification(@NonNull final Context context,
        @NonNull final Bundle notificationBundle, @NonNull final Intent launcherIntent,
        @Nullable final NotificationExtender notificationExtender) {
    final JSONObject alert;
    final int payloadHash;

    try {
        String payload = notificationBundle.getString(CARD_PAYLOAD_KEY);
        if (payload == null) {
            return false;
        }
        payloadHash = payload.hashCode();

        JSONObject payloadObject = new JSONObject(payload);
        alert = payloadObject.optJSONObject("alert") != null ? payloadObject.optJSONObject("alert")
                : new JSONObject();
    } catch (JSONException ex) {
        Log.e(LOG_TAG, "Error while parsing notification bundle JSON", ex);
        return false;
    }

    final boolean[] success = new boolean[1];

    final Thread backgroundThread = new Thread(new Runnable() {
        @Override
        public void run() {
            Looper.prepare();
            prepareCard(context, notificationBundle, new PrepareCallback() {
                @Override
                public void onPrepared(@NonNull Intent presentationIntent) {
                    Intent contentIntent = new Intent(launcherIntent);
                    contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    contentIntent.putExtra(EXTRA_PAYLOAD_INTENT, presentationIntent);

                    NotificationManager manager = (NotificationManager) context
                            .getSystemService(Context.NOTIFICATION_SERVICE);
                    Notification.Builder builder = new Notification.Builder(context)
                            .setSmallIcon(android.R.drawable.ic_dialog_alert)
                            .setContentTitle(alert.optString("title")).setContentText(alert.optString("body"))
                            .setAutoCancel(true)
                            .setContentIntent(PendingIntent.getActivity(context.getApplicationContext(),
                                    payloadHash, contentIntent, PendingIntent.FLAG_ONE_SHOT));

                    if (notificationExtender != null) {
                        builder = notificationExtender.extendNotification(builder);
                    }

                    manager.notify(NOTIFICATION_TAG, payloadHash, builder.getNotification());
                    success[0] = true;
                    Looper.myLooper().quit();
                }

                @Override
                public void onError(@NonNull Exception exception) {
                    Log.e(LOG_TAG, "Error while preparing card", exception);
                    Looper.myLooper().quit();
                }
            });

            Looper.loop();
        }
    });

    backgroundThread.start();

    try {
        backgroundThread.join();
    } catch (InterruptedException ex) {
        Log.e(LOG_TAG, "Failed to wait for background thread", ex);
        return false;
    }
    return success[0];
}

From source file:org.ohmage.sync.OhmageSyncAdapter.java

private void showInstallApkNotification(int id, Builder builder, Intent intent) {
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(getContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent).setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);

    NotificationManager mNotificationManager = (NotificationManager) getContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(id, builder.build());
}

From source file:com.autoupdateapk.AutoUpdateApk.java

protected void raise_notification() {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager) context.getSystemService(ns);

    // nm.cancel( NOTIFICATION_ID ); // tried this, but it just doesn't do
    // the trick =(
    nm.cancelAll();/*  www. j a v a2s  .c o m*/

    String update_file = preferences.getString(UPDATE_FILE, "");
    if (update_file.length() > 0) {
        setChanged();
        notifyObservers(AUTOUPDATE_HAVE_UPDATE);

        // raise notification
        Notification notification = new Notification(appIcon, appName + " update", System.currentTimeMillis());
        notification.flags |= NOTIFICATION_FLAGS;

        CharSequence contentTitle = appName + " update available";
        CharSequence contentText = "Select to install";
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setDataAndType(
                Uri.parse("file://" + context.getFilesDir().getAbsolutePath() + "/" + update_file),
                ANDROID_PACKAGE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        nm.notify(NOTIFICATION_ID, notification);
    } else {
        nm.cancel(NOTIFICATION_ID);
    }
}