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.android.dragonkeyboardfirmwareupdater.KeyboardFirmwareUpdateService.java

private void showBatteryWarningNotification() {
    Log.d(TAG, "showBatteryWarningNotification: " + getKeyboardString());

    Notification batteryWarningNotification = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.notification_battery_warning_title))
            .setContentText(getString(R.string.notification_battery_warning_text))
            .setSmallIcon(R.drawable.ic_battery_warning).setAutoCancel(true).setOnlyAlertOnce(true)
            .setPriority(Notification.PRIORITY_HIGH).setColor(Color.RED)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(getString(R.string.notification_battery_warning_text)))
            .build();//  ww  w. j a  v a  2 s . c om

    // Show the notification via notification manager
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(BATTERY_WARNING_NOTIFICATION_ID, batteryWarningNotification);
}

From source file:net.ben.subsonic.androidapp.util.Util.java

public static void showErrorNotification(final Context context, Handler handler, String title,
        Exception error) {/*www.ja  va2s  . c o  m*/
    final NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    StringBuilder text = new StringBuilder();
    if (error.getMessage() != null) {
        text.append(error.getMessage()).append(" (");
    }
    text.append(error.getClass().getSimpleName());
    if (error.getMessage() != null) {
        text.append(")");
    }

    // Set the icon, scrolling text and timestamp
    final Notification notification = new Notification(android.R.drawable.stat_sys_warning, title,
            System.currentTimeMillis());
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // The PendingIntent to launch our activity if the user selects this notification
    Intent intent = new Intent(context, ErrorActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(Constants.INTENT_EXTRA_NAME_ERROR, title + ".\n\n" + text);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, text, contentIntent);

    // Send the notification.
    handler.post(new Runnable() {
        @Override
        public void run() {
            notificationManager.cancel(Constants.NOTIFICATION_ID_ERROR);
            notificationManager.notify(Constants.NOTIFICATION_ID_ERROR, notification);
        }
    });
}

From source file:com.agilepush.client.SkyMqttCallbackHandler.java

/**
 * Displays a notification in the notification area of the UI
 * //from  w ww .j a  v a  2s  . c  o  m
 * @param context
 *            Context from which to create the notification
 * @param messageString
 *            The string to display to the user as a message
 * @param intent
 *            The intent which will start the activity when the user clicks
 *            the notification
 * @param notificationTitle
 *            The resource reference to the notification title
 */
private void notifcation(String messageString, Intent intent, int notificationTitle) {

    // Get the notification manage which we will use to display the
    // notification
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

    Calendar.getInstance().getTime().toString();

    long when = System.currentTimeMillis();

    // get the notification title from the application's strings.xml file
    CharSequence contentTitle = context.getString(notificationTitle);

    // the message that will be displayed as the ticker
    String ticker = contentTitle + " " + messageString;

    // build the pending intent that will start the appropriate activity
    PendingIntent pendingIntent = PendingIntent.getActivity(context, SkyMqttConstants.showHistory, intent, 0);

    // build the notification
    Builder notificationCompat = new Builder(context);
    notificationCompat.setAutoCancel(true).setContentTitle(contentTitle).setContentIntent(pendingIntent)
            .setContentText(messageString).setTicker(ticker).setWhen(when).setSmallIcon(R.drawable.ic_launcher);

    Notification notification = notificationCompat.build();
    // display the notification
    mNotificationManager.notify(MessageID, notification);
    MessageID++;

}

From source file:com.brejza.matt.habmodem.Dsp_service.java

public void setChaseCarNotification() {
    String body = "Uploading Chase Car Positions";
    String title = "Chase Car";
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_car).setContentTitle(title).setAutoCancel(true)
            .setContentText(body);//from w w  w  .ja v  a 2s  .  com
    // Creates an explicit intent for an Activity in your app
    /*    Intent resultIntent = new Intent(this, Map_Activity.class);
            
                
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(Map_Activity.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(0, mBuilder.build());

}

From source file:gpsalarm.app.service.PostMonitor.java

private void showNotification(String str, String gid) {
    final NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification note = new Notification(R.drawable.status, "Locate@ Tweet", System.currentTimeMillis());
    Intent i = new Intent(this, AlarmEdit3.class);
    i.putExtra("user", prefs.getString("user", null));
    i.putExtra(ReminderHelper.GLOBAL_ID, gid);
    //      i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
    //                      Intent.FLAG_ACTIVITY_SINGLE_TOP);
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

    note.setLatestEventInfo(this, "Locate@ Tweet", str, pi);

    mgr.notify(NOTIFICATION_ID, note);
}

From source file:com.android.dragonkeyboardfirmwareupdater.KeyboardFirmwareUpdateService.java

private void showUpdateNotification() {
    Log.d(TAG, "showUpdateNotification: " + getKeyboardString());

    // Intent for triggering the update confirmation page.
    Intent updateConfirmation = new Intent(this, UpdateConfirmationActivity.class);
    updateConfirmation.putExtra(EXTRA_KEYBOARD_NAME, mKeyboardName);
    updateConfirmation.putExtra(EXTRA_KEYBOARD_ADDRESS, mKeyboardAddress);
    updateConfirmation.putExtra(EXTRA_KEYBOARD_FIRMWARE_VERSION, mKeyboardFirmwareVersion);
    updateConfirmation.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    // Intent for postponing update.
    Intent postponeUpdate = new Intent(ACTION_KEYBOARD_UPDATE_POSTPONED);

    // Wrap intents into pending intents for notification use.
    PendingIntent laterIntent = PendingIntent.getBroadcast(this, 0, postponeUpdate,
            PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent installIntent = PendingIntent.getActivity(this, 0, updateConfirmation,
            PendingIntent.FLAG_CANCEL_CURRENT);

    // Create a notification object with two buttons (actions)
    mUpdateNotification = new NotificationCompat.Builder(this).setCategory(Notification.CATEGORY_SYSTEM)
            .setContentTitle(getString(R.string.notification_update_title))
            .setContentText(getString(R.string.notification_update_text)).setSmallIcon(R.drawable.ic_keyboard)
            .addAction(new NotificationCompat.Action.Builder(R.drawable.ic_later,
                    getString(R.string.notification_update_later), laterIntent).build())
            .addAction(new NotificationCompat.Action.Builder(R.drawable.ic_install,
                    getString(R.string.notification_update_install), installIntent).build())
            .setAutoCancel(true).setOnlyAlertOnce(true).build();

    // Show the notification via notification manager
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(UPDATE_NOTIFICATION_ID, mUpdateNotification);
}

From source file:com.brewcrewfoo.performance.fragments.BatteryInfo.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup root, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.battery_info, root, false);

    mbattery_percent = (TextView) view.findViewById(R.id.batt_percent);
    mbattery_volt = (TextView) view.findViewById(R.id.batt_volt);
    mbattery_status = (TextView) view.findViewById(R.id.batt_status);
    mBattIcon = (ImageView) view.findViewById(R.id.batt_icon);

    if (new File(BAT_VOLT_PATH).exists()) {
        int volt = Integer.parseInt(Helpers.readOneLine(BAT_VOLT_PATH));
        if (volt > 5000)
            volt = (int) Math.round(volt / 1000.0);// in microvolts
        mbattery_volt.setText(volt + " mV");
        mBattIcon.setVisibility(ImageView.GONE);
        mbattery_volt.setVisibility(TextView.VISIBLE);
        mbattery_volt.setOnClickListener(new View.OnClickListener() {
            @Override//from  w  w w  . j  av  a2  s . co  m
            public void onClick(View view) {
                Intent powerUsageIntent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
                if (powerUsageIntent.resolveActivity(context.getPackageManager()) != null)
                    startActivity(powerUsageIntent);
            }
        });
        mbattery_volt.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                mBattIcon.setVisibility(ImageView.VISIBLE);
                mbattery_volt.setVisibility(TextView.GONE);
                return true;
            }
        });
    } else {
        mBattIcon.setVisibility(ImageView.VISIBLE);
        mbattery_volt.setVisibility(TextView.GONE);
        mBattIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    Intent powerUsageIntent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
                    startActivity(powerUsageIntent);
                } catch (Exception e) {
                }
            }
        });
    }

    SeekBar mBlxSlider = (SeekBar) view.findViewById(R.id.blx_slider);
    if (new File(BLX_PATH).exists()) {
        no_settings = false;
        mBlxSlider.setMax(100);

        mBlxVal = (TextView) view.findViewById(R.id.blx_val);
        mBlxVal.setText(getString(R.string.blx_title) + " " + Helpers.readOneLine(BLX_PATH) + "%");

        mBlxSlider.setProgress(Integer.parseInt(Helpers.readOneLine(BLX_PATH)));
        mBlxSlider.setOnSeekBarChangeListener(this);
        Switch mSetOnBoot = (Switch) view.findViewById(R.id.blx_sob);
        mSetOnBoot.setChecked(mPreferences.getBoolean(BLX_SOB, false));
        mSetOnBoot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton v, boolean checked) {
                final SharedPreferences.Editor editor = mPreferences.edit();
                editor.putBoolean(BLX_SOB, checked);
                if (checked) {
                    editor.putInt(PREF_BLX, Integer.parseInt(Helpers.readOneLine(BLX_PATH)));
                }
                editor.commit();
            }
        });
    } else {
        LinearLayout mpart = (LinearLayout) view.findViewById(R.id.blx_layout);
        mpart.setVisibility(LinearLayout.GONE);
    }
    mFastChargePath = Helpers.fastcharge_path();
    if (mFastChargePath != null) {
        no_settings = false;
        mFastchargeOnBoot = (Switch) view.findViewById(R.id.fastcharge_sob);
        mFastchargeOnBoot.setChecked(
                mPreferences.getBoolean(PREF_FASTCHARGE, Helpers.readOneLine(mFastChargePath).equals("1")));
        mFastchargeOnBoot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton v, boolean checked) {
                mPreferences.edit().putBoolean(PREF_FASTCHARGE, checked).apply();
                final NotificationManager nm = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                if (checked) {
                    new CMDProcessor().su.runWaitFor("busybox echo 1 > " + mFastChargePath);
                    Notification n = new Notification.Builder(context)
                            .setContentTitle(context.getText(R.string.app_name))
                            .setContentText(context.getText(R.string.fast_charge_notification_title))
                            .setTicker(context.getText(R.string.fast_charge_notification_title))
                            .setSmallIcon(R.drawable.ic_fastcharge).setWhen(System.currentTimeMillis())
                            .getNotification();
                    //n.flags = Notification.FLAG_NO_CLEAR;
                    nm.notify(1337, n);
                } else {
                    new CMDProcessor().su.runWaitFor("busybox echo 0 > " + mFastChargePath);
                    nm.cancel(1337);
                }
            }
        });
    } else {
        LinearLayout mpart = (LinearLayout) view.findViewById(R.id.fastcharge_layout);
        mpart.setVisibility(LinearLayout.GONE);
    }
    if (no_settings) {
        LinearLayout ns = (LinearLayout) view.findViewById(R.id.no_settings);
        ns.setVisibility(LinearLayout.VISIBLE);
    }

    return view;
}

From source file:com.wso2.mobile.mdm.services.Operation.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*  w  ww  .  ja  v  a 2s  .  c  om*/
private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_stat_gcm;
    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, NotifyActivity.class);
    notificationIntent.putExtra("notification", message);
    // 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_SHOW_LIGHTS;
    notificationManager.notify(0, notification);
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}

From source file:de.steveliedtke.gcm.example.gcm.GCMIntentService.java

public void displayNotification(final Context context, final String message, final boolean vibrationEnabled,
        final boolean permanentNotification) throws JSONException {
    // TODO rewrite this method: throw notification with message

    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

    int icon;/*from   w ww.  j a  v  a  2 s .c om*/
    CharSequence notificationText;
    final CharSequence contentTitle;
    icon = R.drawable.ic_launcher;
    notificationText = context.getString(R.string.new_message);
    contentTitle = context.getString(R.string.new_message);

    long when = System.currentTimeMillis();

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(icon);
    builder.setTicker(notificationText);
    builder.setWhen(when);
    if (message != null && message.length() > 0) {
        builder.setStyle(
                new NotificationCompat.BigTextStyle().setSummaryText(notificationText).bigText(message));
    }
    builder.setAutoCancel(true);
    builder.setContentTitle(contentTitle);
    builder.setContentText(notificationText);

    final Notification notification = builder.build();
    if (vibrationEnabled) {
        notification.defaults |= Notification.DEFAULT_VIBRATE;
    }
    mNotificationManager.notify(82917, notification);
}

From source file:com.portfolio.course.esguti.goubiquitous.mobile.gcm.MyGcmListenerService.java

/**
 *  Put the message into a notification and post it.
 *  This is just one simple example of what you might choose to do with a GCM message.
 *
 * @param message The alert message to be posted.
 *//*from w w w .j  a  va2  s  .  c  o m*/
private void sendNotification(String message) {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

    // Notifications using both a large and a small icon (which yours should!) need the large
    // icon as a bitmap. So we need to create that here from the resource ID, and pass the
    // object along in our notification builder. Generally, you want to use the com.portfolio.course.esguti.goubiquitous.mobile icon as the
    // small icon, so that users understand what com.portfolio.course.esguti.goubiquitous.mobile is triggering this notification.
    Bitmap largeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.art_storm);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.art_clear).setLargeIcon(largeIcon).setContentTitle("Weather Alert!")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message)).setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_HIGH);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}