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.example.jkgan.pmot.service.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received./*from www .  j a  va 2s  . c  o m*/
 */
private void sendNotification(final String message, final String title, String id) {

    String url = MyApplication.getApiUrl() + "/promotions/" + id + "?token="
            + MyApplication.getUser().getToken();
    final Promotion[] promotion = { null };

    final OkHttpClient client = new OkHttpClient();

    final Request request = new Request.Builder().url(url).build();

    //        new Thread(new Runnable() {
    //            @Override
    //            public void run() {
    Response response = null;
    try {
        response = client.newCall(request).execute();
        JSONObject jsnObj2 = new JSONObject(response.body().string());

        promotion[0] = new Promotion(jsnObj2.optString("pName"), jsnObj2.optString("description"),
                jsnObj2.optString("id"),
                jsnObj2.getJSONObject("image").getJSONObject("medium").optString("url"),
                jsnObj2.getJSONObject("image").getJSONObject("small").optString("url"),
                jsnObj2.optString("term_and_condition"), jsnObj2.optString("name"),
                jsnObj2.optString("address"), jsnObj2.optString("sId"), getDate(jsnObj2.optString("starts_at")),
                getDate(jsnObj2.optString("expires_at")), jsnObj2.optString("phone"));

        Intent intent = new Intent(getApplicationContext(), PromotionActivity.class);
        intent.putExtra("NAME", promotion[0].getName());
        intent.putExtra("SHOP_ID", promotion[0].getId());
        intent.putExtra("IMAGE", promotion[0].getImage());
        intent.putExtra("DESCRIPTION", promotion[0].getDescription());
        intent.putExtra("TNC", promotion[0].getTnc());
        intent.putExtra("SHOP_NAME", promotion[0].getShop().getName());
        intent.putExtra("ADDRESS", promotion[0].getShop().getAddress());
        intent.putExtra("START", promotion[0].getStarts_at());
        intent.putExtra("EXPIRE", promotion[0].getExpires_at());
        intent.putExtra("PHONE", promotion[0].getShop().getPhone());
        intent.putExtra("SUBSCRIBED", true);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0 /* Request code */,
                intent, PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        if (numMessages >= 1) {
            notificationTitle += (", " + title);

            notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.mipmap.ic_launcher).setContentTitle((numMessages + 1) + " new promotions")
                    .setContentText(notificationTitle).setAutoCancel(true).setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
            numMessages = 0;

            notificationBuilder.setContentText(message).setNumber(++numMessages);

        } else {
            notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title).setContentText(message)
                    .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

            notificationTitle = title;
            numMessages++;
            //            notificationBuilder.setContentText(message)
            //                    .setNumber(++numMessages);
        }

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

        // On gnre un nombre alatoire pour pouvoir afficher plusieurs notifications
        notificationManager.notify(notifyID, notificationBuilder.build());
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.getmarco.weatherstationviewer.gcm.StationGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.//from www  .  j a v a2s .c om
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, StationListActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Station update").setContentText(message)
            .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:de.taxilof.UulmLoginAgent.java

/**
 * notify the User in Statusbar/*from  w w w  .  j a  v a 2s  . c o m*/
 */
private void notify(String subject, String message, boolean errorIcon) {
    // build notification with notifyString
    Notification notifyDetails;
    mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (errorIcon) {
        notifyDetails = new Notification(R.drawable.icon_red, subject, System.currentTimeMillis());
    } else {
        notifyDetails = new Notification(R.drawable.icon, subject, System.currentTimeMillis());
    }
    PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    notifyDetails.setLatestEventInfo(context, subject, message, myIntent);
    notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
}

From source file:butter.droid.base.torrent.TorrentService.java

public void startForeground() {
    if (Foreground.get().isForeground())
        return;//from  www  .j  a  va2  s.  co  m
    if (mCurrentActivityClass == null)
        return;

    Intent notificationIntent = new Intent(this, mCurrentActivityClass);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Intent stopIntent = new Intent();
    stopIntent.setAction(TorrentBroadcastReceiver.STOP);
    PendingIntent pendingStopIntent = PendingIntent.getBroadcast(this, TorrentBroadcastReceiver.REQUEST_CODE,
            stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Action stopAction = new NotificationCompat.Action.Builder(
            R.drawable.abc_ic_clear_mtrl_alpha, getString(R.string.stop), pendingStopIntent).build();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notif_logo)
            .setContentTitle(getString(R.string.app_name) + " - " + getString(R.string.running))
            .setContentText(getString(R.string.tap_to_resume)).setOngoing(true).setOnlyAlertOnce(true)
            .setPriority(Notification.PRIORITY_LOW).setContentIntent(pendingIntent).addAction(stopAction)
            .setCategory(NotificationCompat.CATEGORY_SERVICE);

    if (mStreamStatus != null && mIsReady) {
        String downloadSpeed;
        DecimalFormat df = new DecimalFormat("#############0.00");
        if (mStreamStatus.downloadSpeed / 1024 < 1000) {
            downloadSpeed = df.format(mStreamStatus.downloadSpeed / 1024) + " KB/s";
        } else {
            downloadSpeed = df.format(mStreamStatus.downloadSpeed / (1024 * 1024)) + " MB/s";
        }
        String progress = df.format(mStreamStatus.progress);
        builder.setContentText(progress + "%, " + downloadSpeed);
    }

    Notification notification = builder.build();

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

    notifManager.notify(NOTIFICATION_ID, notification);
    startForeground(NOTIFICATION_ID, notification);

    if (mUpdateTimer == null) {
        mUpdateTimer = new Timer();
        mUpdateTimer.scheduleAtFixedRate(new UpdateTask(), 5000, 5000);
    }
}

From source file:by.zatta.pilight.connection.ConnectionService.java

@Override
public void onCreate() {
    Log.v(TAG, "onCreate");
    super.onCreate();
    ctx = this;/*from ww w  . j  ava 2 s.  c o  m*/
    aCtx = getApplicationContext();

    SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String language = getPrefs.getString("languagePref", "unknown");
    if (!language.equals("unknown"))
        makeLocale(language);

    IntentFilter filter = new IntentFilter();
    filter.addAction("pilight-reconnect");
    filter.addAction("pilight-kill-service");
    filter.addAction("pilight-switch-device");
    this.registerReceiver(mMessageReceiver, filter);

    mNotMan = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    isConnectionUp = makeConnection();
    isDestroying = false;
    Log.v(TAG, "onCreate done");

}

From source file:edu.mines.letschat.GcmIntentService.java

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

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra(MainActivity.EXTRA_NOTIFICATION_RETRIEVE, senderID);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    notificationCounter++;/*from   ww  w. ja  v  a2  s .  co  m*/

    if (messages.size() > 1) {
        msg = msg + "...";
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setVibrate(new long[] { 0, 500, 250, 500, 250 }).setLights(Color.BLUE, 200, 200)
            .setSmallIcon(R.drawable.logo).setContentTitle("Let's Chat Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setNumber(notificationCounter)
            .setTicker(msg).setContentText(msg);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    // Sets a title for the Inbox style big view
    inboxStyle.setBigContentTitle("Unread messages (" + notificationCounter + "):");
    // Moves events into the big view
    ArrayList<String> temp = new ArrayList<String>();
    for (int i = 0; i < 5; ++i) {
        if (i == messages.size()) {
            break;
        }
        temp.add(messages.get(i));
    }
    Collections.reverse(temp);
    messages = temp;
    for (String s : messages) {
        inboxStyle.addLine(s);
    }

    mBuilder.setStyle(inboxStyle);

    mBuilder.setAutoCancel(true);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    //        AwesomeAdapter.animate = true;
}

From source file:com.jaguarlandrover.auto.remote.vehicleentry.RviService.java

static void sendNotification(Context ctx, String action, String... extras) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    NotificationManager nm = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    boolean fire = prefs.getBoolean("pref_fire_notifications", true);

    if (!fire)//w w  w.  j  av a2s . c o  m
        return;

    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx).setSmallIcon(R.drawable.rvi_not)
            .setAutoCancel(true).setContentTitle(ctx.getResources().getString(R.string.app_name))
            .setContentText(action);

    Intent targetIntent = new Intent(ctx, LockActivity.class);
    int j = 0;
    for (String ex : extras) {
        targetIntent.putExtra("_extra" + (++j), ex);
        targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    }
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, targetIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    nm.notify(0, builder.build());
}

From source file:arun.com.chromer.webheads.WebHeadService.java

@NonNull
@Override/*from w w w  .  ja va  2s .  c  o  m*/
public Notification getNotification() {
    if (Utils.ANDROID_OREO) {
        final NotificationChannel channel = new NotificationChannel(WebHeadService.class.getName(),
                getString(R.string.web_heads_service), NotificationManager.IMPORTANCE_MIN);
        channel.setDescription(getString(R.string.app_detection_notification_channel_description));
        final NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
    final PendingIntent contentIntent = PendingIntent.getBroadcast(this, 0,
            new Intent(ACTION_STOP_WEBHEAD_SERVICE), FLAG_UPDATE_CURRENT);
    final PendingIntent contextActivity = PendingIntent.getBroadcast(this, 0,
            new Intent(ACTION_OPEN_CONTEXT_ACTIVITY), FLAG_UPDATE_CURRENT);
    final PendingIntent newTab = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_OPEN_NEW_TAB),
            FLAG_UPDATE_CURRENT);
    Notification notification = new NotificationCompat.Builder(this, WebHeadService.class.getName())
            .setSmallIcon(R.drawable.ic_chromer_notification).setPriority(PRIORITY_MIN)
            .setContentText(getString(R.string.tap_close_all))
            .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .addAction(R.drawable.ic_add, getText(R.string.open_new_tab), newTab)
            .addAction(R.drawable.ic_list, getText(R.string.manage), contextActivity)
            .setContentTitle(getString(R.string.web_heads_service)).setContentIntent(contentIntent)
            .setAutoCancel(false).setLocalOnly(true).build();
    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
    return notification;
}

From source file:com.sublimis.urgentcallfilter.Magic.java

@SuppressWarnings("deprecation")
private void notification() {
    if (MyPreference.isNotifications()) {
        if (mContext != null) {
            NotificationManager notificationManager = (NotificationManager) mContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);

            if (notificationManager != null) {
                Notification notification = new Notification(R.drawable.icon, null, System.currentTimeMillis());
                Intent notificationIntent = new Intent(mContext, ActivityMain.class);

                if (notification != null && notificationIntent != null) {
                    notificationIntent.setFlags(notificationIntent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
                    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, notificationIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT);

                    notification.setLatestEventInfo(mContext,
                            mContext.getResources().getString(R.string.notification_title),
                            mContext.getResources().getString(R.string.notification_text), pendingIntent);

                    notificationManager.notify(0, notification);
                }//from   www  .ja  v a  2s.  c  o m
            }
        }
    }
}