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.riksof.a320.c2dm.common.C2DMReceiver.java

@Override
protected void onMessage(Context contxt, Intent intent) {

    String unValidatedURL = intent.getStringExtra("payload");
    Log.w("C2DMReceiver", unValidatedURL);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    int icon = R.drawable.ic_launcher; // icon from resources

    CharSequence tickerText = "You got message !!!"; // ticker-text
    long when = System.currentTimeMillis(); // notification time
    Context context = getApplicationContext(); // application Context

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

    CharSequence contentTitle = "Norification Received !";

    // the next two lines initialize the Notification, using the configurations above
    Notification notification = new Notification(icon, tickerText, when);
    notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.ledARGB = 0xff00ff00;//from   w  w  w .  j  a v a  2s.  c  o m
    notification.ledOnMS = 300;
    notification.ledOffMS = 1000;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.setLatestEventInfo(context, contentTitle, unValidatedURL, contentIntent);
    notificationManager.notify(10001, notification);

    Cache.getInstance().remove(unValidatedURL);
    FileCache fc = new FileCache(context);
    fc.clear();

    Log.w("C2DMReceiver", "finish");
}

From source file:org.zywx.wbpalmstar.platform.push.PushRecieveMsgReceiver.java

private void oldPushNotification(Context context, Intent intent) {
    PushReportUtility.log("oldPushNotification->isForground = " + EBrowserActivity.isForground);
    if (EBrowserActivity.isForground) {
        if (mContext != null) {
            intent.putExtra("ntype", F_TYPE_PUSH);
            ((EBrowserActivity) mContext).handleIntent(intent);
        }/*  w w  w .j  av  a 2 s .  co  m*/
    } else {
        CharSequence tickerText = intent.getStringExtra("title"); // ????
        Resources res = context.getResources();
        int icon = res.getIdentifier("icon", "drawable", intent.getPackage());
        long when = System.currentTimeMillis(); // ?
        // ??Nofification

        String notifyTitle = null;
        String pushMessage = intent.getStringExtra("message");
        String value = intent.getStringExtra("data"); // ??json
        try {
            JSONObject bodyJson = new JSONObject(value);
            notifyTitle = bodyJson.getString("msgName");// ?
        } catch (Exception e) {
            PushReportUtility.oe("onReceive", e);
        }
        if (TextUtils.isEmpty(notifyTitle)) {
            notifyTitle = intent.getStringExtra("widgetName");// msgNamewidgetName?
        }
        if (TextUtils.isEmpty(notifyTitle)) {
            notifyTitle = "APPCAN";// widgetNameAPPCAN?
        }
        CharSequence contentTitle = notifyTitle; // ?
        Intent notificationIntent = new Intent(context, EBrowserActivity.class); // ??Activity
        notificationIntent.putExtra("data", value);
        notificationIntent.putExtra("message", pushMessage);
        notificationIntent.putExtra("ntype", F_TYPE_PUSH);
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
        Notification notification = new Notification(icon, tickerText, when);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        if (Build.VERSION.SDK_INT >= 16) {
            try {
                Field priorityField = Notification.class.getField("priority");
                priorityField.setAccessible(true);
                priorityField.set(notification, 1);
            } catch (Exception e) {
                PushReportUtility.oe("onReceive", e);
            }
        }
        PendingIntent contentIntent = PendingIntent.getActivity(context, notificationNB, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(context, contentTitle, tickerText, contentIntent);
        // NotificationNotificationManager
        mNotificationManager.notify(notificationNB, notification);
        notificationNB++;
    }
}

From source file:com.att.android.arodatacollector.main.AROCollectorService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Notification mAROnotification = mApp.getARONotification();
    NotificationManager mAROnotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mAROnotificationManager.notify(ARODataCollector.NOTIFICATION_ID, mAROnotification);
    startForeground(ARODataCollector.NOTIFICATION_ID, mAROnotification);

    if (DEBUG) {/*ww w  .j  a  v a 2  s .co m*/
        Log.d(TAG, "AROCollectorService started in foreground at timestamp:" + System.currentTimeMillis());
    }

    return (START_NOT_STICKY);

}

From source file:com.adarshahd.indianrailinfo.donate.PNRTracker.java

private void showNotification(String title, String contentText, ArrayList<String> inboxVal,
        String activityToStart) {
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
    notificationBuilder.setSmallIcon(R.drawable.irctc).setContentTitle(title).setContentText(contentText);
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    for (String str : inboxVal) {
        inboxStyle.addLine(str);//from   ww w.java  2 s  .c  o m
    }
    notificationBuilder.setStyle(inboxStyle);
    notificationBuilder.setOnlyAlertOnce(true);
    if (!activityToStart.equals("")) {
        Intent intent = new Intent(mContext, PNRStat.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(Presenter.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(intent);
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(pendingIntent);
    }
    NotificationManager notificationManager = (NotificationManager) mContext
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notify = notificationBuilder.build();
    //notify.sound = Uri.parse("content://settings/system/notification_sound");
    notify.sound = Uri
            .parse(mPref.getString("notification_ringtone", "content://settings/system/notification_sound"));
    notify.ledARGB = 0xFF00FF00;
    notify.ledOffMS = 10000;
    notify.ledOnMS = 1500;
    notify.flags |= Notification.FLAG_AUTO_CANCEL;
    notify.flags |= Notification.FLAG_SHOW_LIGHTS;
    if (mPref.getBoolean("notification_vibrate", true)) {
        //Should find out a way to vibrate
        long[] pattern = { 0, 200 };
        notify.vibrate = pattern;
    }
    /*notify.flags = Notification.DEFAULT_ALL;
    notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    notify.flags |= Notification.DEFAULT_SOUND;
    notify.flags |= Notification.DEFAULT_LIGHTS;*/
    notificationManager.notify(0, notify);
}

From source file:io.clh.lrt.androidservice.TraceListenerService.java

private void setNotification(String msg, boolean ongoing) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    int icon = R.drawable.ic_launcher;
    CharSequence notiText = msg;/*from   w ww .j a  va2 s  .c  om*/
    long meow = System.currentTimeMillis();

    Notification notification = new Notification(icon, notiText, meow);

    Context context = getApplicationContext();
    CharSequence contentTitle = "LRT notification";
    CharSequence contentText = msg;
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    int SERVER_DATA_RECEIVED = 1;
    if (ongoing) {
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        startForeground(1337, notification);
    } else {
        notificationManager.notify(SERVER_DATA_RECEIVED, notification);
    }
}

From source file:be.ac.ucl.lfsab1509.llncampus.services.AlarmService.java

/**
 * Send an alert to the user for the event e.
 * /* w w  w  .  jav  a2 s  . c  o m*/
 * @param e
 *          Event to notify to the user.
 */
private void sendAlert(Event e) {
    final NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    Time currentTime = new Time();
    currentTime.setToNow();

    long nbMin = e.getBeginTime().toMillis(false) / 60L / 1000L - currentTime.toMillis(false) / 60L / 1000L;

    final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(android.R.drawable.ic_dialog_alert)
            .setContentTitle(e.getDetail(Event.COURSE) + " " + getString(R.string.begins_in) + " " + nbMin + " "
                    + getString(R.string.minutes))
            .setContentText(e.getDetail(Event.ROOM) + " - " + e.getDetail(Event.TITLE)).setAutoCancel(true);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String ringtone = preferences.getString(SettingsActivity.NOTIFY_RINGTONE, null);
    if (ringtone == null) {
        notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    } else {
        notificationBuilder.setSound(Uri.parse(ringtone));
    }
    notificationManager.notify(1, notificationBuilder.build());
}

From source file:io.scalac.locationprovider.SendMockLocationService.java

/**
 * Post a notification to the notification bar. The title of the notification is fixed, but
 * the content comes from the input argument contentText. The notification does not contain
 * a content Intent, because the only destination it could have would be the main activity.
 * It's better to use the Recents button to go to the existing main activity.
 *
 * @param contentText Text to use for the notification content (main line of expanded
 *                    notification).//from w  w w . ja  v a 2s. com
 */
private void postNotification(String contentText) {

    // An instance of NotificationManager is needed to issue a notification
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    /*
     * Instantiate a new notification builder, using the API version that's backwards
     * compatible to platform version 4.
     */
    NotificationCompat.Builder builder;

    // Get the notification title
    String contentTitle = this.getString(R.string.notification_title_test_start);

    // Add values to the builder
    builder = new NotificationCompat.Builder(this).setAutoCancel(false).setSmallIcon(R.drawable.ic_notify)
            .setContentTitle(contentTitle).setContentText(contentText);

    /*
     * Post the notification. All notifications from InjectMockLocationService have the same
     * ID, so posting a new notification overwrites the old one.
     */
    notificationManager.notify(0, builder.build());
}

From source file:com.android.mms.transaction.MessagingNotification.java

/**
 * This method sends a notification to NotificationManager to display
 * an dialog indicating the message memory is full.
 *//*from w  w w  .ja  va 2 s .c  o m*/
private static void sendFullNotification(Context context) {
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    String title = context.getString(R.string.sms_full_title);
    String description = context.getString(R.string.sms_full_body);
    PendingIntent intent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    Notification notification = new Notification();
    notification.icon = R.drawable.stat_notify_sms_failed;
    notification.tickerText = title;
    notification.setLatestEventInfo(context, title, description, intent);
    nm.notify(FULL_NOTIFICATION_ID, notification);
}

From source file:ch.carteggio.provider.sync.NotificationService.java

private void showFailureNotification() {

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Carteggio network error").setSmallIcon(android.R.drawable.stat_notify_error);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(NetworkStatusActivity.class);
    stackBuilder.addNextIntent(new Intent(this, NetworkStatusActivity.class));

    notifyBuilder.setContentIntent(stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));

    if (mSendFailure && mReceiveFailure) {
        notifyBuilder.setContentText("There was a problem while delivering and receiving messages");
    } else if (mSendFailure) {
        notifyBuilder.setContentText("There was a problem while delivering messages");
    } else if (mReceiveFailure) {
        notifyBuilder.setContentText("There was a problem while receiving messages");
    }/*from ww  w.j ava 2s.c om*/

    mNotificationManager.notify(ERROR_NOTIFICATION_ID, notifyBuilder.build());

}

From source file:org.c99.SyncProviderDemo.EventosSyncAdapterService.java

private static void generateNotification(Evento evento, Context context) {
    NotificationManager mNotificationManager;
    int numMessages = 0;
    Log.i("Start", "notification");

    /* Invoking the default notification service */
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);

    mBuilder.setContentTitle("Novo evento");
    mBuilder.setContentText(evento.getNome());
    mBuilder.setTicker("Evento !!!");
    mBuilder.setSmallIcon(R.drawable.logo);

    /* Increase notification number every time a new notification arrives */
    mBuilder.setNumber(++numMessages);//from   w  ww. ja  v a 2 s  . com

    /* Creates an explicit intent for an Activity in your app */
    Intent resultIntent = new Intent(context, NavigationDrawer.class);
    resultIntent.setAction("EVENTO"); //tentando linkar
    Bundle bundle = new Bundle();
    bundle.putSerializable("evento", evento);
    resultIntent.putExtras(bundle);
    // fim arrumar a inteao

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(NavigationDrawer.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);

    mNotificationManager =
            //                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            //                (NotificationManager) getActivity().getApplication().
            //                        getSystemService(getActivity().getApplication().NOTIFICATION_SERVICE);

            /* notificationID allows you to update the notification later on. */
            //                (NotificationManager) getApplication().getSystemService(NOTIFICATION_SERVICE);
            (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    mNotificationManager.notify(evento.getCodigo(), mBuilder.build());
}