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.orange.oidc.tim.service.Service.java

private void showNotification(boolean bProtect) {
    Logd(TAG, "show protected icon " + bProtect);

    // this is it, we'll build the notification!
    // in the addAction method, if you don't want any icon, just set the first param to 0
    Notification mNotification = null;

    if (bProtect) {
        mNotification = new Notification.Builder(this)

                .setContentTitle("TIM").setContentText("privacy protected").setSmallIcon(R.drawable.masked_on)
                .setAutoCancel(false).build();
    } else {/*w  w  w  .  jav a 2 s . c  o  m*/
        mNotification = new Notification.Builder(this)

                .setContentTitle("TIM").setContentText("privacy not protected")
                .setSmallIcon(R.drawable.masked_off).setAutoCancel(false).build();
    }

    // to make it non clearable
    mNotification.flags |= Notification.FLAG_NO_CLEAR;

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // If you want to hide the notification after it was selected, do the code below
    // myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, mNotification);
}

From source file:jp.co.ipublishing.esnavi.impl.gcm.GcmIntentService.java

/**
 * Notification??/*from  w  ww  . j ava2 s . c  o m*/
 *
 * @param alert 
 */
private void sendNotification(@NonNull Alert alert) {
    final NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    final Intent resultIntent = new Intent(this, MapActivity.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent contentIntent = PendingIntent.getActivity(this, 1, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    final Notification.Builder builder = new Notification.Builder(this).setWhen(System.currentTimeMillis())
            .setContentIntent(contentIntent)
            .setDefaults(
                    Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
            .setAutoCancel(true).setTicker(alert.getHeadlineBody()).setContentText(alert.getHeadlineBody());

    onPreSendNotification(builder, alert);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        notificationManager.notify(NOTIFICATION_ID, builder.build());
    } else {
        notificationManager.notify(NOTIFICATION_ID, builder.getNotification());
    }
}

From source file:com.beepscore.android.sunshine.gcm.MyGcmListenerService.java

/**
 *  Put the message into a notification and post it on this Android device.
 *  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 ww.ja  va2s  . 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.
    // Create largeIcon from the resource ID
    Bitmap largeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.art_storm);

    // Generally, you want to set the small icon to the app icon,
    // so that users understand what app is triggering this notification.

    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());
}

From source file:com.aero2.android.DefaultActivities.SmogMapActivity.java

public void notifyServiceComplete() {
    //Set the boolean in cache to state true i.e. the service is complete.
    SharedPreferences afterServiceCompletion = getApplicationContext()
            .getSharedPreferences("AFTER_SERVICE_COMPLETED", Context.MODE_WORLD_WRITEABLE);
    SharedPreferences.Editor afterServiceCompletionEditor = afterServiceCompletion.edit();
    afterServiceCompletionEditor.putBoolean("AFTER_SERVICE_COMPLETED", true);
    afterServiceCompletionEditor.commit();

    //Notify the User that the download service has been completed
    int mNotificationId = 235;
    NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Data AerO2")
            .setContentText("Smog Data Download Complete");
    NotificationManager mNotifyMgr = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
    Intent resultIntent = new Intent(this, SmogMapActivity.class);

    //What to do if the notification is clicked??
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    mNotifyMgr.notify(mNotificationId, mBuilder.build());

    /*//www .  ja va 2 s .com
     Once the service is complete notify the system that service isn't
     completed so that it doesn't refresh the activity over and over again.
     sorry this might seen a bit wiered but this is how it works.
    */
    SharedPreferences serviceStatus = getApplicationContext().getSharedPreferences("SERVICE_COMPLETED",
            Context.MODE_WORLD_WRITEABLE);
    SharedPreferences.Editor serviceStatusEdit = serviceStatus.edit();
    serviceStatusEdit.putBoolean("SERVICE_COMPLETED", false);
    serviceStatusEdit.commit();

}

From source file:com.airbop.library.simple.AirBopGCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*from www  .ja  v  a 2s . co  m*/
private static void generateNotification(Context context, String title, String message) {

    AirBopManifestSettings airBop_settings = CommonUtilities.loadDataFromManifest(context);
    //int icon = R.drawable.ic_stat_gcm;
    int icon = 0;
    Resources res = context.getResources();
    if (res != null) {
        //icon = res.getIdentifier(airBop_settings.mDefaultNotificationIcon, null, null);
        icon = airBop_settings.mNotificationIcon;
    }
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    //if ((title == null) || (title.equals(""))) {
    if (title == null) {
        title = airBop_settings.mDefaultNotificationTitle;
    }
    Class intent_class = null;
    if (context != null) {
        ClassLoader class_loader = context.getClassLoader();
        if (class_loader != null) {
            try {
                if (airBop_settings.mDefaultNotificationClass != null) {
                    intent_class = Class.forName(airBop_settings.mDefaultNotificationClass);
                }
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //Log.i(TAG, "intent_class: " + intent_class);
        }
    }
    Intent notificationIntent = null;
    if (intent_class != null) {
        notificationIntent = new Intent(context, intent_class);
    } else {
        notificationIntent = new Intent(Intent.ACTION_VIEW);
    }
    // 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 notification = new NotificationCompat.Builder(context).setContentTitle(title)
            .setContentText(message).setContentIntent(intent).setSmallIcon(icon).setWhen(when)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message)).build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

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

private void showProximityNotification(String string) {
    final NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification note = new Notification(R.drawable.status, "Friend@ notification!",
            System.currentTimeMillis());
    Intent i = new Intent(this, AlertList.class);

    //      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, "Friend@ notification", string, pi);

    mgr.notify(NOTIFICATION_ID, note);

}

From source file:com.coderming.weatherwatch.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  ww . ja va  2  s . co 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 app icon as the
    // small icon, so that users understand what app 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());
}

From source file:org.wso2.emm.agent.services.operation.OperationManager.java

/**
 * Lock the device./*from w w w . java  2  s  . c o m*/
 *
 * @param operation - Operation object.
 */
public void lockDevice(org.wso2.emm.agent.beans.Operation operation) throws AndroidAgentException {
    operation.setStatus(resources.getString(R.string.operation_value_completed));
    resultBuilder.build(operation);
    JSONObject inputData;
    String message = null;
    boolean isHardLockEnabled = false;
    try {
        if (operation.getPayLoad() != null) {
            inputData = new JSONObject(operation.getPayLoad().toString());
            message = inputData.getString(Constants.ADMIN_MESSAGE);
            isHardLockEnabled = inputData.getBoolean(Constants.IS_HARD_LOCK_ENABLED);
        }
    } catch (JSONException e) {
        operation.setStatus(resources.getString(R.string.operation_value_error));
        operation.setOperationResponse("Error in parsing LOCK payload.");
        resultBuilder.build(operation);
        throw new AndroidAgentException("Invalid JSON format.", e);
    }
    if (isHardLockEnabled && Constants.SYSTEM_APP_ENABLED) {
        if (message == null || message.isEmpty()) {
            message = resources.getString(R.string.txt_lock_activity);
        }
        Preference.putBoolean(context, Constants.IS_LOCKED, true);
        Preference.putString(context, Constants.LOCK_MESSAGE, message);
        operation.setStatus(resources.getString(R.string.operation_value_completed));
        resultBuilder.build(operation);
        enableHardLock(message, operation);
    } else {
        operation.setStatus(resources.getString(R.string.operation_value_completed));
        resultBuilder.build(operation);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher).setContentTitle(context.getString(R.string.alert_message))
                .setContentText(message).setAutoCancel(true)
                .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(), 0));
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, mBuilder.build());
        devicePolicyManager.lockNow();
    }

    if (Constants.DEBUG_MODE_ENABLED) {
        Log.d(TAG, "Device locked");
    }
}

From source file:com.example.bryan.sunshine.app.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.
 *//* w w w.j a v  a 2  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 app icon as the
    // small icon, so that users understand what app 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());
}

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

public void setDecoderRunningNotification() {
    String body = "RTTY decoder is processing audio";
    String title = "Decoder Running";
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_decoderrunning).setContentTitle(title).setContentText(body);

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(1, mBuilder.build());

}