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.cbtec.eliademy.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*w  w w .  j a  v a2s .co m*/

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.icon;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, Eliademy.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

From source file:com.arubadesignweb.app.appoverview.util.GcmIntentService.java

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

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Application.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.push_icon)
            .setContentTitle(this.getApplicationContext().getString(R.string.url))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

    mBuilder.setAutoCancel(true);//from  w w  w. j ava 2  s . c  o m
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

From source file:de.electricdynamite.pasty.GCMIntentService.java

@SuppressLint("NewApi")
@Override//from w w  w.j  a v a 2  s  .  co  m
protected void onMessage(Context context, Intent intent) {
    if (prefs == null)
        this.prefs = new PastyPreferencesProvider(context);
    final Bundle extras = intent.getExtras();
    if (extras == null) {
        Log.i(TAG, "onMessage(): Empty intent received");
        return;
    }
    final String mItemId = extras.getString("itemId");
    final String mItemStr = extras.getString("item");
    final int mEventId = Integer.parseInt(extras.getString("eventId"));
    if (mItemId == null || mItemStr == null || mItemId == "" || mItemStr == "") {
        Log.i(TAG, "onMessage(): Invalid intent received");
        return;
    }
    if (LOCAL_LOG)
        Log.v(TAG, "onMessage(): Received message for event: " + mEventId);
    switch (mEventId) {
    case EVENT_ITEM_ADDED:
        final String lastItemId = prefs.getLastItem();
        if (mItemId.equals(lastItemId))
            return;
        if (client == null) {
            client = new PastyClient(prefs.getRESTBaseURL(), true);
            client.setUsername(prefs.getUsername());
            client.setPassword(prefs.getPassword());
        }

        final ClipboardItem mItem = new ClipboardItem(mItemId, mItemStr);
        final Boolean mPush = prefs.getPush();
        final Boolean mCopyToClipboard = prefs.getPushCopyToClipboard();
        final Boolean mNotify = prefs.getPushNotify();
        if (mPush == true) {
            if (mCopyToClipboard == true) {
                if (mItem.getText() != "") {
                    Intent resultIntent = new Intent(this, CopyService.class);
                    resultIntent.putExtra("de.electricdynamite.pasty.itemId", mItem.getId());
                    resultIntent.putExtra("de.electricdynamite.pasty.item", mItem.getText());
                    resultIntent.putExtra("de.electricdynamite.pasty.notify", mNotify);
                    startService(resultIntent);
                    resultIntent = null;
                }
            } else {
                if (mNotify == true) {
                    String contentText = String.format(getString(R.string.notification_event_add_text),
                            mItem.getText());
                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_stat_pasty)
                            .setContentTitle(getString(R.string.notification_event_add_title))
                            .setContentText(contentText).setAutoCancel(Boolean.TRUE);
                    // Creates an explicit intent for an Activity in your app
                    Intent resultIntent = new Intent(this, CopyService.class);
                    resultIntent.putExtra("de.electricdynamite.pasty.itemId", mItem.getId());
                    resultIntent.putExtra("de.electricdynamite.pasty.item", mItem.getText());
                    resultIntent.putExtra("de.electricdynamite.pasty.notify", mNotify);
                    PendingIntent resultPendingIntent = PendingIntent.getService(context, 0, resultIntent,
                            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(PastySharedStatics.NOTIFICATION_ID, mBuilder.build());
                }
            }
            JSONArray clipboard = null;
            try {
                clipboard = client.getClipboard();
                cacheClipboard(clipboard);
            } catch (PastyException e) {
                if (LOCAL_LOG)
                    Log.v(TAG, "Could not get clipboard: " + e.getMessage());
            }

        }
        break;
    default:
        Log.i(TAG, "onMessage(): Unsupported event: " + mEventId);
        break;
    }
    /* TODO Make ClipboardFragment react to changes from GCMIntentService */
}

From source file:me.calebjones.blogsite.network.PostDownloader.java

private void notificationService() {
    //Create an Intent for the BroadcastReceiver
    Intent buttonIntent = new Intent(this, ButtonReceiver.class);
    buttonIntent.putExtra("notificationId", NOTIF_ID);

    //Create the PendingIntent
    PendingIntent cancelNotification = PendingIntent.getBroadcast(this, 0, buttonIntent, 0);

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

    mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setContentTitle("The Jones Theory").setOngoing(true)
            .setContentText("Preparing to download Science...")
            .addAction(R.drawable.ic_action_close, "Hide", cancelNotification)
            .setSmallIcon(R.drawable.ic_action_file_download)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));

    // Sets an activity indicator for an operation of indeterminate length
    mBuilder.setProgress(0, 0, true);//from  w  w  w. j a  va 2 s. co  m
    // Issues the notification
    mNotifyManager.notify(NOTIF_ID, mBuilder.build());
}

From source file:be.ac.ucl.lfsab1509.llncampus.ADE.java

/**
 * Start information update from ADE.//from   w  ww . j  ava2s. c  o  m
 * 
 * @param sa
 *            Activity that launches the update thread.
 * @param updateRunnable
 *            Runnable that launches the display update.
 * @param handler
 *            Handler to allow the display update at the end of the execution.
 */
public static void runUpdateADE(final ScheduleActivity sa, final Handler handler,
        final Runnable updateRunnable) {
    final Resources r = sa.getResources();

    final NotificationManager nm = (NotificationManager) sa.getSystemService(Context.NOTIFICATION_SERVICE);

    final NotificationCompat.Builder nb = new NotificationCompat.Builder(sa)
            .setSmallIcon(android.R.drawable.stat_sys_download)
            .setContentTitle(r.getString(R.string.download_from_ADE))
            .setContentText(r.getString(R.string.download_progress)).setAutoCancel(true);

    final NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle(r.getString(R.string.download_from_ADE));

    nm.notify(NOTIFY_ID, nb.build());
    new Thread(new Runnable() {
        @Override
        public void run() {

            /*
             * Fetching of code of courses to load.
             */
            ArrayList<Course> courses = Course.getList();

            if (courses == null || courses.isEmpty()) {
                // Take the context of the ScheduleActivity
                SharedPreferences preferences = new SecurePreferences(sa);
                String username = preferences.getString("username", null);
                String password = preferences.getString("password", null);
                if (username != null && password != null) {
                    UCLouvain.downloadCoursesFromUCLouvain(sa, username, password, new Runnable() {
                        public void run() {
                            runUpdateADE(sa, handler, updateRunnable);
                        }
                    }, handler);
                }

            }

            /*
             * Number of weeks. To download all the schedule, the numbers go from 0 to 51
             * (0 = beginning of first semester, 51 = ending of the September exams session).
             */
            String weeks = getWeeks();

            /*
             * Fetching data from ADE and updating the database
             */
            int nbError = 0;
            int i = 0;
            ArrayList<Event> events;
            for (Course course : courses) {
                i++;
                nb.setProgress(courses.size(), i, false);
                nb.setContentText(r.getString(R.string.download_for) + " " + course.getCourseCode() + "...");
                nm.notify(NOTIFY_ID, nb.build());
                events = ADE.getInfo(course.getCourseCode(), weeks);
                if (events == null) {
                    nbError++;
                    inboxStyle.addLine(course.getCourseCode() + " : " + r.getString(R.string.download_error));
                } else {
                    // Removing old data
                    LLNCampus.getDatabase().delete("Horaire", "COURSE = ?",
                            new String[] { course.getCourseCode() });
                    // Adding new data
                    for (Event e : events) {
                        ContentValues cv = e.toContentValues();
                        cv.put("COURSE", course.getCourseCode());
                        if (LLNCampus.getDatabase().insert("Horaire", cv) < 0) {
                            nbError++;
                        }
                    }
                    inboxStyle.addLine(course.getCourseCode() + " : " + events.size() + " "
                            + r.getString(R.string.events));
                    nb.setStyle(inboxStyle);
                }
            }

            nb.setProgress(courses.size(), courses.size(), false);

            if (nbError == 0) {
                nb.setContentText(r.getString(R.string.download_done));
                inboxStyle.setBigContentTitle(r.getString(R.string.download_done));
                nb.setSmallIcon(android.R.drawable.stat_sys_download_done);
                nb.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            } else {
                nb.setContentText(r.getString(R.string.download_done) + ". " + r.getString(R.string.nb_error)
                        + nbError + " :/");
                inboxStyle.setBigContentTitle(r.getString(R.string.download_done) + ". "
                        + r.getString(R.string.nb_error) + nbError + " :/");
                nb.setSmallIcon(android.R.drawable.stat_notify_error);
                nb.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            }
            nb.setStyle(inboxStyle);
            nm.notify(NOTIFY_ID, nb.build());

            handler.post(updateRunnable);

        }
    }).start();
}

From source file:de.wikilab.android.friendica01.FileUploadService.java

@Override
protected void onHandleIntent(Intent intent) {
    Log.i("Andfrnd/UploadFile", "onHandleIntent exec");

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

    //Instantiate the Notification:
    CharSequence tickerText = "Uploading...";
    Notification notification = new Notification(R.drawable.arrow_up, tickerText, System.currentTimeMillis());
    notification.flags |= Notification.FLAG_ONGOING_EVENT;

    //Define the Notification's expanded message and Intent:
    Context context = getApplicationContext();
    PendingIntent nullIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    notification.setLatestEventInfo(context, "Upload in progress...", "You are notified here when it completes",
            nullIntent);//from  w  w  w  .j av a2s.  c o  m

    //Pass the Notification to the NotificationManager:
    mNotificationManager.notify(UPLOAD_PROGRESS_ID, notification);
    /*
    final TwLogin login = new TwLogin();
    login.initialize(FileUploadService.this);
    login.doLogin(FileUploadService.this, null, false);
            
    if (!login.isLoginOK()) {
       showFailMsg(FileUploadService.this, "Invalid login data or no network connection");
       return;
    }
    */
    Bundle intentPara = intent.getExtras();
    fileToUpload = (Uri) intentPara.getParcelable(Intent.EXTRA_STREAM);
    descText = intentPara.getString(EXTRA_DESCTEXT);
    subject = intentPara.getString(Intent.EXTRA_SUBJECT);

    if (targetFilename == null || targetFilename.equals(""))
        targetFilename = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".txt";

    String fileSpec = Max.getRealPathFromURI(FileUploadService.this, fileToUpload);

    String tempFile = Max.IMG_CACHE_DIR + "/imgUploadTemp_" + System.currentTimeMillis() + ".jpg";
    Max.resizeImage(fileSpec, tempFile, 1024, 768);

    try {
        Log.i("Andfrnd/UploadFile", "before uploadFile");
        final TwAjax uploader = new TwAjax(FileUploadService.this, true, true);
        uploader.addPostFile(new TwAjax.PostFile("media", targetFilename, tempFile));
        uploader.addPostData("status", descText);
        uploader.addPostData("title", subject);
        uploader.addPostData("source",
                "<a href='http://friendica-for-android.wiki-lab.net'>Friendica for Android</a>");
        uploader.uploadFile(Max.getServer(this) + "/api/statuses/update", null);
        Log.i("Andfrnd/UploadFile", "after uploadFile");
        Log.i("Andfrnd/UploadFile", "isSuccess() = " + uploader.isSuccess());
        Log.i("Andfrnd/UploadFile", "getError() = " + uploader.getError());

        mNotificationManager.cancel(UPLOAD_PROGRESS_ID);
        if (uploader.isSuccess() && uploader.getError() == null) {
            JSONObject result = null;
            try {
                Log.i("Andfrnd/UploadFile", "JSON RESULT: " + uploader.getHttpCode());
                result = (JSONObject) uploader.getJsonResult();

                String postedText = result.getString("text");
                showSuccessMsg(FileUploadService.this);

            } catch (Exception e) {
                String errMes = e.getMessage() + " | " + uploader.getResult();
                if (result != null)
                    try {
                        errMes = result.getString("error");
                    } catch (JSONException fuuuuJava) {
                    }

                showFailMsg(FileUploadService.this, errMes);

                e.printStackTrace();
            }
        } else if (uploader.getError() != null) {
            showFailMsg(FileUploadService.this, uploader.getError().toString());
        } else {
            showFailMsg(FileUploadService.this, uploader.getResult());
        }

    } finally {
        new File(tempFile).delete();
    }
}

From source file:com.rampgreen.caretakermobile.GcmIntentService.java

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

    Bundle bundle = new Bundle();
    bundle.putString(Constants.NOTIFICATION_MSG, msg);
    bundle.putString(Constants.CALLED_COMPONENT, Constants.SERVICE_GCM_INTENT);

    Intent notificationIntent = new Intent(this, ActivityNotification.class);
    notificationIntent.putExtras(bundle);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle(notificationTitle)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(NOTIFICATION_ID, notification);
}

From source file:de.hackerspacebremen.push.PushIntentService.java

public void displayNotification(final Context context, final SpaceData data, final boolean vibrationEnabled,
        final boolean permanentNotification) throws JSONException {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

    final boolean permanent = permanentNotification && data.isSpaceOpen();

    String timeString = context.getString(R.string.unknown);
    Date time = data.getTime();/*from  ww w . j  a v a2  s .c o  m*/
    if (time != null) {
        timeString = SpeakingDateFormat.format(time);
    }

    int icon;
    CharSequence notificationText;
    final CharSequence contentTitle;
    if (data.isSpaceOpen()) {
        icon = R.drawable.notification_open;
        notificationText = context.getString(R.string.space_open, timeString);
        contentTitle = context.getString(R.string.space_open_simple);
    } else {
        icon = R.drawable.notification_closed;
        notificationText = context.getString(R.string.space_closed, timeString);
        contentTitle = context.getString(R.string.space_closed_simple);
    }

    long when = System.currentTimeMillis();

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(icon);
    builder.setTicker(notificationText);
    builder.setWhen(when);
    if (data.getMessage() != null && data.getMessage().length() > 0) {
        builder.setStyle(new NotificationCompat.BigTextStyle().setSummaryText(notificationText)
                .bigText("Nachricht: \n" + data.getMessage()));
    }
    if (!permanent) {
        builder.setAutoCancel(true);
    }
    builder.setContentTitle(contentTitle);
    builder.setContentText(notificationText);
    Intent notificationIntent = new Intent(context, StartActivity.class);
    notificationIntent.putExtra("status_json", SpaceDataJsonParser.parse(data).toString());
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    builder.setContentIntent(contentIntent);

    final Notification notification = builder.build();
    if (vibrationEnabled) {
        notification.defaults |= Notification.DEFAULT_VIBRATE;
    }
    if (permanent) {
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
    }
    mNotificationManager.notify(Constants.NOTIFICATION_ID, notification);
}

From source file:com.app.poke.poke.GcmIntentService.java

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

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivityPhone.class),
            0);/*from w ww.ja  v  a 2s  .co m*/

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.powered_by_google_dark).setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

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

From source file:br.ajmarques.cordova.plugin.localnotification.Receiver.java

/**
 * Zeigt die Notification an./*w  w  w .  j  ava 2 s. c  om*/
 */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void showNotification(NotificationCompat.Builder notification) {
    NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    int id = 0;

    try {
        id = Integer.parseInt(options.getId());
    } catch (Exception e) {
    }

    if (Build.VERSION.SDK_INT < 16) {
        // build notification for HoneyComb to ICS
        mgr.notify(id, notification.getNotification());
    } else if (Build.VERSION.SDK_INT > 15) {
        // Notification for Jellybean and above
        mgr.notify(id, notification.build());
    }
}