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: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:be.uhasselt.privacypolice.NotificationHandler.java

NotificationHandler(Context ctx) {
    this.ctx = ctx;
    notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
}

From source file:bucci.dev.freestyle.NotificationCreator.java

public static void createTimerRunningNotification(Context context, String startPauseButtonState, long timeLeft,
        TimerType timerType, boolean extraButtonVisibleState) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.bft_icon_gray)
            .setContentTitle(context.getString(R.string.notification_timer_running_text)).setAutoCancel(true)
            .setOngoing(true);// w ww.j a  v  a2 s.  c  o  m

    Intent resultIntent = new Intent(context, TimerActivity.class);
    resultIntent.putExtra(StartActivity.TIMER_TYPE, timerType);
    resultIntent.putExtra(TimerActivity.START_PAUSE_STATE_PARAM, startPauseButtonState);
    resultIntent.putExtra(TimerActivity.TIME_LEFT_PARAM, timeLeft);
    if (extraButtonVisibleState)
        resultIntent.putExtra(TimerActivity.SHOW_EXTRA_ROUND_BUTTON_PARAM, true);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(TimerActivity.class);
    stackBuilder.addNextIntent(resultIntent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_TIMER_RUNNING, builder.build());
}

From source file:augsburg.se.alltagsguide.gcm.command.AnnouncementCommand.java

private void displayNotification(Context context, String message) {
    Ln.i("Displaying notification: " + message);
    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(0,
            new NotificationCompat.Builder(context).setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher).setTicker(message)
                    .setContentTitle(context.getString(R.string.app_name)).setContentText(message)
                    .setColor(ContextCompat.getColor(context, R.color.primary))
                    .setContentIntent(PendingIntent.getActivity(context, 0,
                            new Intent(context, OverviewActivity.class).setFlags(
                                    Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                            0))/*from   w w w .ja  v  a 2s.c  om*/
                    .setAutoCancel(true).build());
}

From source file:json2notification.app.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    jsonView = (TextView) findViewById(R.id.json);

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String wikiJson = readFile("wiki.json");
    jsonView.setText(wikiJson);/*from w  w w .  j  a  v a2s.  c  o  m*/
    new AsyncTask<String, Void, Void>() {
        @Override
        public Void doInBackground(String... jsons) {
            android.util.Log.d("json2notification-app", "json:" + jsons[0]);

            //Notification n = Json2Notification.from(MainActivity.this).with(jsons[0]).notification();
            //notificationManager.notify(1, n);
            //android.util.Log.d("json2notification-app", "notify:" + n);

            AndroidNotification androidNotification = AndroidNotification.parse(MainActivity.this, jsons[0]);
            android.util.Log.d("json2notification-app", "notify:" + androidNotification.android.notification);
            notificationManager.notify(1, androidNotification.android.notification);
            //android.util.Log.d("json2notification-app", "serialize:" + androidNotification.serialize());
            return null;
        }
    }.execute(wikiJson);
}

From source file:org.ametro.util.WebUtil.java

public static void downloadFileAsync(final Context appContext, final File path, final URI uri,
        final File temp) {

    final DownloadContext context = new DownloadContext();
    context.Path = path;/*from   w  w  w  . j  av a2 s . c  o m*/
    context.IsCanceled = false;
    context.IsUnpackFinished = false;
    context.IsFailed = false;

    final NotificationManager notificationManager = (NotificationManager) appContext
            .getSystemService(Context.NOTIFICATION_SERVICE);

    final Handler handler = new Handler();

    final Runnable updateProgress = new Runnable() {
        public void run() {
            if (context.Notification == null) {
                context.Notification = new Notification(android.R.drawable.stat_sys_download,
                        "Downloading icons", System.currentTimeMillis());
                context.Notification.flags = Notification.FLAG_NO_CLEAR;
                Intent notificationIntent = new Intent();
                context.ContentIntent = PendingIntent.getActivity(appContext, 0, notificationIntent, 0);
            }
            if (context.IsFailed) {
                notificationManager.cancelAll();
                context.Notification = new Notification(android.R.drawable.stat_sys_warning,
                        "Icons download failed", System.currentTimeMillis());
                context.Notification.setLatestEventInfo(appContext, "aMetro", "Icons downloaded failed",
                        context.ContentIntent);
                notificationManager.notify(2, context.Notification);

            } else if (context.IsUnpackFinished) {
                notificationManager.cancelAll();
                context.Notification = new Notification(android.R.drawable.stat_sys_download_done,
                        "Icons unpacked", System.currentTimeMillis());
                context.Notification.setLatestEventInfo(appContext, "aMetro", "Icons downloaded and unpacked.",
                        context.ContentIntent);
                notificationManager.notify(3, context.Notification);

            } else if (context.Position == 0 && context.Total == 0) {
                context.Notification.setLatestEventInfo(appContext, "aMetro",
                        "Download icons: connecting server", context.ContentIntent);
                notificationManager.notify(1, context.Notification);
            } else if (context.Position < context.Total) {
                context.Notification.setLatestEventInfo(appContext, "aMetro",
                        "Download icons: " + context.Position + "/" + context.Total, context.ContentIntent);
                notificationManager.notify(1, context.Notification);
            } else {
                context.Notification.setLatestEventInfo(appContext, "aMetro", "Icons unpacking",
                        context.ContentIntent);
                notificationManager.notify(1, context.Notification);
            }
        }
    };

    final Thread async = new Thread() {
        public void run() {
            WebUtil.downloadFile(context, uri, temp, false, new IDownloadListener() {

                public void onBegin(Object context, File file) {
                    DownloadContext downloadContext = (DownloadContext) context;
                    downloadContext.Total = 0;
                    downloadContext.Position = 0;
                    handler.removeCallbacks(updateProgress);
                    handler.post(updateProgress);
                }

                public boolean onUpdate(Object context, long position, long total) {
                    DownloadContext downloadContext = (DownloadContext) context;
                    downloadContext.Total = total;
                    downloadContext.Position = position;
                    handler.removeCallbacks(updateProgress);
                    handler.post(updateProgress);
                    return !downloadContext.IsCanceled;
                }

                public void onDone(Object context, File file) throws Exception {
                    DownloadContext downloadContext = (DownloadContext) context;
                    File path = downloadContext.Path;
                    ZipUtil.unzip(file, path);
                    downloadContext.IsUnpackFinished = true;
                    handler.removeCallbacks(updateProgress);
                    handler.post(updateProgress);
                }

                public void onCanceled(Object context, File file) {
                    DownloadContext downloadContext = (DownloadContext) context;
                    downloadContext.IsCanceled = true;
                    handler.removeCallbacks(updateProgress);
                    handler.post(updateProgress);
                }

                public void onFailed(Object context, File file, Throwable reason) {
                    DownloadContext downloadContext = (DownloadContext) context;
                    downloadContext.IsFailed = true;
                    handler.removeCallbacks(updateProgress);
                    handler.post(updateProgress);
                }

            });
        };
    };
    async.start();
}

From source file:GeofenceIntentService.java

private void sendNotification() {
    Log.i("GeofenceIntentService", "sendNotification()");
    Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Geofence Alert")
            .setContentText("GEOFENCE_TRANSITION_DWELL").setSound(notificationSoundUri)
            .setLights(Color.BLUE, 500, 500);
    NotificationManager notificationManager = (NotificationManager) getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

From source file:be.ppareit.swiftp.gui.FsNotification.java

private void setupNotification(Context context) {
    Cat.d("Setting up the notification");
    // Get NotificationManager reference
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager) context.getSystemService(ns);

    // get ip address
    InetAddress address = FsService.getLocalInetAddress();
    if (address == null) {
        Cat.w("Unable to retrieve the local ip address");
        return;// w  ww .  jav  a 2 s .c  o  m
    }
    String iptext = "ftp://" + address.getHostAddress() + ":" + FsSettings.getPortNumber() + "/";

    // Instantiate a Notification
    int icon = R.mipmap.notification;
    CharSequence tickerText = String.format(context.getString(R.string.notification_server_starting), iptext);
    long when = System.currentTimeMillis();

    // Define Notification's message and Intent
    CharSequence contentTitle = context.getString(R.string.notification_title);
    CharSequence contentText = String.format(context.getString(R.string.notification_text), iptext);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    int stopIcon = android.R.drawable.ic_menu_close_clear_cancel;
    CharSequence stopText = context.getString(R.string.notification_stop_text);
    Intent stopIntent = new Intent(FsService.ACTION_STOP_FTPSERVER);
    PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context, 0, stopIntent,
            PendingIntent.FLAG_ONE_SHOT);

    int preferenceIcon = android.R.drawable.ic_menu_preferences;
    CharSequence preferenceText = context.getString(R.string.notif_settings_text);
    Intent preferenceIntent = new Intent(context, MainActivity.class);
    preferenceIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent preferencePendingIntent = PendingIntent.getActivity(context, 0, preferenceIntent, 0);

    Notification notification = new NotificationCompat.Builder(context).setContentTitle(contentTitle)
            .setContentText(contentText).setContentIntent(contentIntent).setSmallIcon(icon)
            .setTicker(tickerText).setWhen(when).setOngoing(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setCategory(NotificationCompat.CATEGORY_SERVICE).setPriority(NotificationCompat.PRIORITY_MAX)
            .addAction(stopIcon, stopText, stopPendingIntent)
            .addAction(preferenceIcon, preferenceText, preferencePendingIntent).setShowWhen(false).build();

    // Pass Notification to NotificationManager
    nm.notify(NOTIFICATION_ID, notification);

    Cat.d("Notification setup done");
}

From source file:net.eledge.android.europeana.gui.notification.receiver.UrlButtonReceiver.java

@Override
public void onReceive(Context context, Intent intent) {

    int notificationId = intent.getIntExtra(PARAM_NOTIFICATIONID, Config.NOTIFICATION_NEWBLOG);
    String url = intent.getStringExtra(PARAM_URL);

    if (StringUtils.isNoneBlank(url)) {
        try {/*from   ww  w. j  av  a  2s . com*/
            PendingIntent.getActivity(context, 0, new Intent(Intent.ACTION_VIEW, Uri.parse(url)),
                    PendingIntent.FLAG_UPDATE_CURRENT).send();
        } catch (PendingIntent.CanceledException e) {
            Log.e(UrlButtonReceiver.class.getSimpleName(), e.getMessage(), e);
        }
    }

    final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(notificationId);
}

From source file:com.krayzk9s.imgurholo.services.DownloadService.java

@Override
protected void onHandleIntent(Intent intent) {
    final NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    ids = intent.getParcelableArrayListExtra("ids");
    albumName = "/";
    downloaded = 0;/*  w  w w . java  2  s.  c o  m*/
    if (ids.size() > 0) {
        albumName += intent.getStringExtra("albumName") + "/";
        File myDirectory = new File(
                android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                albumName);
        if (!myDirectory.exists()) {
            myDirectory.mkdirs();
        }
    }
    for (int i = 0; i < ids.size(); i++) {
        try {
            final String type = ids.get(i).getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TYPE)
                    .split("/")[1];
            final String id = ids.get(i).getJSONObject().getString("id");
            final String link = ids.get(i).getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_LINK);
            Log.d("data", ids.get(i).getJSONObject().toString());
            Log.d(ImgurHoloActivity.IMAGE_DATA_TYPE,
                    ids.get(0).getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TYPE).split("/")[1]);
            Log.d("id", ids.get(i).getJSONObject().getString("id"));
            Log.d(ImgurHoloActivity.IMAGE_DATA_LINK,
                    ids.get(0).getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_LINK));
            final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
            notificationBuilder.setContentTitle(getString(R.string.picture_download))
                    .setContentText(getString(R.string.download_in_progress))
                    .setSmallIcon(R.drawable.icon_desaturated);
            Ion.with(getApplicationContext(), link).progress(new ProgressCallback() {
                @Override
                public void onProgress(int i, int i2) {
                    notificationBuilder.setProgress(i2, i, false);
                }
            }).write(new File(
                    android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                            + albumName + id + "." + type))
                    .setCallback(new FutureCallback<File>() {
                        @Override
                        public void onCompleted(Exception e, File file) {
                            if (file == null)
                                return;
                            downloaded += 1;
                            if (downloaded == ids.size()) {
                                NotificationCompat.Builder notificationComplete = new NotificationCompat.Builder(
                                        getApplicationContext());
                                if (ids.size() == 1) {
                                    Intent viewImageIntent = new Intent(Intent.ACTION_VIEW);
                                    viewImageIntent.setDataAndType(Uri.fromFile(file), "image/*");
                                    Intent shareIntent = new Intent(Intent.ACTION_SEND);
                                    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                                    shareIntent.setType("image/*");
                                    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                                    PendingIntent viewImagePendingIntent = PendingIntent.getActivity(
                                            getApplicationContext(), (int) System.currentTimeMillis(),
                                            viewImageIntent, 0);
                                    PendingIntent sharePendingIntent = PendingIntent.getActivity(
                                            getApplicationContext(), (int) System.currentTimeMillis(),
                                            shareIntent, 0);
                                    notificationComplete.setContentTitle(getString(R.string.download_complete))
                                            .setSmallIcon(R.drawable.icon_desaturated)
                                            .setContentText(String.format(getString(R.string.download_progress),
                                                    downloaded))
                                            .setContentIntent(viewImagePendingIntent)
                                            .addAction(R.drawable.dark_social_share, getString(R.string.share),
                                                    sharePendingIntent);
                                } else {
                                    Intent i = new Intent(Intent.ACTION_PICK);
                                    i.setDataAndType(
                                            Uri.fromFile(new File(
                                                    android.os.Environment.getExternalStoragePublicDirectory(
                                                            Environment.DIRECTORY_PICTURES) + albumName)),
                                            "image/*");
                                    PendingIntent viewImagePendingIntent = PendingIntent.getActivity(
                                            getApplicationContext(), (int) System.currentTimeMillis(), i, 0);
                                    notificationComplete.setContentTitle(getString(R.string.download_complete))
                                            .setSmallIcon(R.drawable.icon_desaturated).setContentText(String
                                                    .format(getString(R.string.download_progress), downloaded))
                                            .setContentIntent(viewImagePendingIntent);
                                }
                                notificationManager.cancel(0);
                                notificationManager.cancel(1);
                                notificationManager.notify(1, notificationComplete.build());
                            }
                            MediaScannerConnection.scanFile(getApplicationContext(),
                                    new String[] { android.os.Environment.getExternalStoragePublicDirectory(
                                            Environment.DIRECTORY_PICTURES) + albumName + id + "." + type },
                                    null, new MediaScannerConnection.OnScanCompletedListener() {
                                        @Override
                                        public void onScanCompleted(final String path, final Uri uri) {
                                            Log.i("Scanning", String.format("Scanned path %s -> URI = %s", path,
                                                    uri.toString()));
                                        }
                                    });
                        }
                    });
            notificationManager.notify(0, notificationBuilder.build());
        } catch (JSONException e) {
            Log.e("Error!", e.toString());
        }
    }
}