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.app.ntuc.notifs.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received./*from  www  .  ja  va  2 s  . c  o m*/
 */
private void sendNotification(String body, String title, Bundle bundle) {
    Intent intent = new Intent(this, MainActivity2_.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
    bigText.bigText(bundle.toString());
    bigText.setBigContentTitle(body);
    bigText.setSummaryText(title);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_ic_notification).setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    notificationBuilder.setContentTitle(body).setContentText(title);

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

    notificationManager.notify(6969, notificationBuilder.build());
}

From source file:org.roman.findme.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.// ww w.  j  av  a 2s .  c  o m
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, MessageActivity.class);
    intent.putExtra("message", message);
    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.drawable.ic_stat_ic_notification).setContentTitle("Message").setContentText(message)
            .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

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

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

    if (sharedPreferences.getBoolean("display_message", true)) {
        displayMessage(message);
    }
}

From source file:com.android.talkbacktests.testsession.NotificationTest.java

private void showSimpleNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext())
            .setSmallIcon(android.R.drawable.ic_notification_overlay).setAutoCancel(true)
            .setContentTitle(getString(R.string.normal_notification_title))
            .setContentText(getString(R.string.normal_notification_text))
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getContext());
    stackBuilder.addParentStack(MainActivity.class);

    Intent resultIntent = new Intent(getContext(), MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);

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

    NotificationManager notificationManager = (NotificationManager) getContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID_MAIN_MENU, builder.build());
}

From source file:andre.com.datapushandroid.services.FCMService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */// w  w  w. j  a  v a 2  s .  com
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.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("FCM Message").setContentText(messageBody)
            .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

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

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

From source file:com.android.talkbacktests.testsession.NotificationTest.java

private void showCustomNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext()).setAutoCancel(true)
            .setSmallIcon(android.R.drawable.ic_notification_overlay)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

    RemoteViews contentView = new RemoteViews(getContext().getPackageName(), R.layout.custom_notification);
    contentView.setImageViewResource(R.id.notification_image, android.R.drawable.ic_dialog_email);
    contentView.setTextViewText(R.id.notification_title, getString(R.string.custom_notification_title));
    contentView.setTextViewText(R.id.notification_text, getString(R.string.custom_notification_text));
    builder.setContent(contentView);/*w  ww.j a v a2 s  .  c o m*/

    Intent resultIntent = new Intent(getContext(), MainActivity.class);
    resultIntent.setAction(Intent.ACTION_MAIN);
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, resultIntent, 0);
    builder.setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID_LAST_VIEW, builder.build());
}

From source file:com.barelabor.barelabor.gcm.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received./*from  www  . ja va  2  s.  co  m*/
 */
private void sendNotification(String repairArrayString, String highCostArrayString,
        String averageCostArrayString, String lowCostArrayString, String lowPrice, String highPrice,
        String avgPrice) {

    //        String lowPrice = "0", highPrice = "0", avgPrice = "0";
    //        try {
    //            JSONArray data = new JSONArray(message);
    //            lowPrice = data.getJSONObject(0).getString("lowCost");
    //            avgPrice = data.getJSONObject(0).getString("averageCost");
    //            highPrice = data.getJSONObject(0).getString("highCost");
    //        } catch (JSONException e) {
    //            e.printStackTrace();
    //        }

    Intent intent = new Intent(this, MenuActivity.class);
    intent.putExtra("low_price", lowPrice);
    intent.putExtra("high_price", highPrice);
    intent.putExtra("avg_price", avgPrice);
    intent.putExtra("repairArrayString", repairArrayString);
    intent.putExtra("highCostArrayString", highCostArrayString);
    intent.putExtra("averageCostArrayString", averageCostArrayString);
    intent.putExtra("lowCostArrayString", lowCostArrayString);
    intent.putExtra("notification", true);

    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("BareLabor")
            .setContentText("The pricing for your printed estimate has arrived").setAutoCancel(true)
            .setSound(defaultSoundUri).setContentIntent(pendingIntent);

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

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

From source file:com.cliqz.browser.gcm.MessageListenerService.java

/**
 * Create and show a simple notification containing the received GCM message. Does nothing if
 * the notifications are disabled in the preferences.
 *
 * @param title GCM message received./*from  ww w. ja  v a 2 s  . c o m*/
 * @param url   url
 */
private void sendNewsNotification(int newType, String title, String url, String country) {
    if (!preferenceManager.getNewsNotificationEnabled() || country == null
            || !country.equals(preferenceManager.getCountryChoice().countryCode)) {
        return;
    }

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    intent.putExtra(Constants.NOTIFICATION_CLICKED, true);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Intent deleteIntent = new Intent(this, NotificationDismissedReceiver.class);
    PendingIntent deletePendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0,
            deleteIntent, 0);

    final Uri uri = Uri.parse(url);
    final String host = uri.getHost();
    final String domain = UrlUtils.getTopDomain(url);
    final Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    final NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
    style.bigText(title);
    final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notification_news).setContentTitle(domain)
            .setCategory(NotificationCompat.CATEGORY_RECOMMENDATION).setContentText(title).setAutoCancel(true)
            .setSound(defaultSoundUri).setContentIntent(pendingIntent).setDeleteIntent(deletePendingIntent)
            .setStyle(style);

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

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

From source file:android.example.com.squawker.fcm.SquawkFirebaseMessageService.java

/**
 * Create and show a simple notification containing the received FCM message
 *
 * @param data Map which has the message data in it
 *//*from  w  w w . j a va2  s  .c  om*/
private void sendNotification(Map<String, String> data) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    // Create the pending intent to launch the activity
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String author = data.get(JSON_KEY_AUTHOR);
    String message = data.get(JSON_KEY_MESSAGE);

    // If the message is longer than the max number of characters we want in our
    // notification, truncate it and add the unicode character for ellipsis
    if (message.length() > NOTIFICATION_MAX_CHARACTERS) {
        message = message.substring(0, NOTIFICATION_MAX_CHARACTERS) + "\u2026";
    }

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_duck)
            .setContentTitle(String.format(getString(R.string.notification_message), author))
            .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:com.handshake.notifications.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *///from  www .ja  va 2s  .co  m
private void sendNotification(final Bundle data, long userId, boolean isContact) {
    Intent intent;
    if (userId == 0) {
        intent = new Intent(this, MainActivity.class);
    } else if (isContact) {
        intent = new Intent(this, ContactUserProfileActivity.class);
    } else {
        intent = new Intent(this, GenericUserProfileActivity.class);
    }
    intent.putExtra("userId", userId);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    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.drawable.ic_stat_ic_notification).setContentTitle("Handshake")
            .setContentText(data.getString("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:com.craftsilicon.littlecabrider.GCMIntentService.java

private void generateNotificationNew(Context context, String message) {
    final Notification.Builder builder = new Notification.Builder(this);
    builder.setDefaults(/*from   ww w .j  a v  a  2  s .  c o m*/
            Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
    builder.setStyle(new Notification.BigTextStyle(builder).bigText(message)
            .setBigContentTitle(context.getString(R.string.app_name)))
            .setContentTitle(context.getString(R.string.app_name)).setContentText(message)
            .setSmallIcon(R.drawable.ic_launcher);
    builder.setAutoCancel(true);
    Intent notificationIntent = new Intent(context, MainDrawerActivity.class);
    notificationIntent.putExtra("fromNotification", "notification");
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(intent);
    final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(0, builder.build());
}