Example usage for android.app Notification DEFAULT_SOUND

List of usage examples for android.app Notification DEFAULT_SOUND

Introduction

In this page you can find the example usage for android.app Notification DEFAULT_SOUND.

Prototype

int DEFAULT_SOUND

To view the source code for android.app Notification DEFAULT_SOUND.

Click Source Link

Document

Use the default notification sound.

Usage

From source file:me.acristoffers.tracker.AlarmReceiver.java

@Override
public void statusUpdated(final Package pkg) {
    final String code = pkg.getCod();
    final int count = pkg.getSteps().size();

    if (countSteps.get(code) < count) {
        pkg.save();/*w  w w .  j  a v a  2 s .  c  om*/

        final NotificationManager notificationManager = (NotificationManager) context.get()
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            final String title = context.get().getString(R.string.notification_package_updated_title,
                    pkg.getName());
            final String message = context.get().getString(R.string.notification_package_updated_body,
                    pkg.getName());

            final Intent intent = new Intent(context.get(), PackageDetailsActivity.class);
            intent.putExtra(PackageDetailsActivity.PACKAGE_CODE, code);
            final PendingIntent pendingIntent = PendingIntent.getActivity(context.get(), 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            final Bitmap icon = BitmapFactory.decodeResource(context.get().getResources(),
                    R.mipmap.ic_launcher);

            final NotificationCompat.Builder builder = new NotificationCompat.Builder(context.get());
            builder.setTicker(title).setContentTitle(title).setContentText(message)
                    .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND
                            | Notification.DEFAULT_VIBRATE)
                    .setContentIntent(pendingIntent).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(icon)
                    .setWhen(System.currentTimeMillis()).setAutoCancel(true);

            final Notification notification = builder.build();

            final NotificationManager nm = (NotificationManager) context.get()
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            nm.notify(pkg.getId(), notification);
        }
    }
}

From source file:com.anrlabs.locationreminder.GeoFenceReceiver.java

private void sendNotification(String title) {

    Intent notificationIntent = new Intent(context, ShowReminder.class);
    notificationIntent.putExtra("notificationId", strinIds[0]);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(ShowReminder.class);
    stackBuilder.addNextIntent(notificationIntent);
    PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    //int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
    // PendingIntent notificationPendingIntent = PendingIntent.getActivity(context, iUniqueId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle(title).setContentText("Click here to open app")
            .setContentIntent(notificationPendingIntent).setTicker("You have a new Reminder");
    builder.setAutoCancel(true);//from w  ww .j a va2 s  .  co  m
    pref = context.getSharedPreferences("alert", 0);
    if (pref.getBoolean("check", false)) {
        long[] vibrate = { 0, 100, 200, 200, 200, 200 };
        builder.setVibrate(vibrate);

    } else
        builder.setDefaults(Notification.DEFAULT_SOUND);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}

From source file:com.playpalgames.app.GcmIntentService.java

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

    Intent intent = new Intent(this.getApplicationContext(), StartActivity_.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.putExtra("COMMAND", command);
    intent.setAction(Long.toString(System.currentTimeMillis()));

    PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ball)
            .setContentTitle("Penalty!").setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationMessage))
            .setContentText(notificationMessage);

    mBuilder.setContentIntent(contentIntent);
    Notification note = mBuilder.build();
    note.defaults |= Notification.DEFAULT_VIBRATE;
    note.defaults |= Notification.DEFAULT_SOUND;

    mNotificationManager.notify(NOTIFICATION_ID, note);
}

From source file:com.android.example.notificationshowcase.NotificationService.java

@Override
protected void onHandleIntent(Intent intent) {
    ArrayList<Notification> mNotifications = new ArrayList<Notification>();
    NotificationManager noMa = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    int bigtextId = mNotifications.size();
    mNotifications.add(makeBigTextNotification(this, 0, bigtextId, System.currentTimeMillis()));

    int uploadId = mNotifications.size();
    long uploadWhen = System.currentTimeMillis();
    mNotifications.add(makeUploadNotification(this, 10, uploadWhen));

    Notification phoneCall = new NotificationCompat.Builder(this).setContentTitle("Incoming call")
            .setContentText("Matias Duarte").setLargeIcon(getBitmap(this, R.drawable.matias_hed))
            .setSmallIcon(R.drawable.stat_sys_phone_call).setDefaults(Notification.DEFAULT_SOUND)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Matias"))
            .addAction(R.drawable.ic_dial_action_call, "Answer",
                    ToastService.getPendingIntent(this, "call answered"))
            .addAction(R.drawable.ic_end_call, "Ignore", ToastService.getPendingIntent(this, "call ignored"))
            .setAutoCancel(true).build();
    phoneCall.flags |= Notification.FLAG_INSISTENT;
    mNotifications.add(phoneCall);/*from w  w w .  ja v  a 2s  . c o m*/

    mNotifications.add(
            new NotificationCompat.Builder(this).setContentTitle("Stopwatch PRO").setContentText("Counting up")
                    .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Stopwatch"))
                    .setSmallIcon(R.drawable.stat_notify_alarm).setUsesChronometer(true).build());

    mNotifications.add(
            new NotificationCompat.Builder(this).setContentTitle("J Planning").setContentText("The Botcave")
                    .setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.stat_notify_calendar)
                    .setContentIntent(ToastService.getPendingIntent(this, "Clicked on calendar event"))
                    .setContentInfo("7PM")
                    .addAction(R.drawable.stat_notify_snooze, "+10 min",
                            ToastService.getPendingIntent(this, "snoozed 10 min"))
                    .addAction(R.drawable.stat_notify_snooze_longer, "+1 hour",
                            ToastService.getPendingIntent(this, "snoozed 1 hr"))
                    .addAction(R.drawable.stat_notify_email, "Email",
                            makeEmailIntent(this, "gabec@example.com,mcleron@example.com,dsandler@example.com"))
                    .build());

    BitmapDrawable d = (BitmapDrawable) getResources().getDrawable(R.drawable.romainguy_rockaway);
    mNotifications.add(new NotificationCompat.BigPictureStyle(new NotificationCompat.Builder(this)
            .setContentTitle("Romain Guy")
            .setContentText("I was lucky to find a Canon 5D Mk III at a local Bay Area "
                    + "store last week but I had not been able to try it in the field "
                    + "until tonight. After a few days of rain the sky finally cleared "
                    + "up. Rockaway Beach did not disappoint and I was finally able to "
                    + "see what my new camera feels like when shooting landscapes.")
            .setSmallIcon(R.drawable.ic_stat_gplus)
            .setContentIntent(ToastService.getPendingIntent(this, "Clicked on bigPicture"))
            .setLargeIcon(getBitmap(this, R.drawable.romainguy_hed))
            .addAction(R.drawable.add, "Add to Gallery",
                    ToastService.getPendingIntent(this, "added! (just kidding)"))
            .setSubText("talk rocks!")).bigPicture(d.getBitmap()).build());

    // Note: this may conflict with real email notifications
    StyleSpan bold = new StyleSpan(Typeface.BOLD);
    SpannableString line1 = new SpannableString("Alice: hey there!");
    line1.setSpan(bold, 0, 5, 0);
    SpannableString line2 = new SpannableString("Bob: hi there!");
    line2.setSpan(bold, 0, 3, 0);
    SpannableString line3 = new SpannableString("Charlie: Iz IN UR EMAILZ!!");
    line3.setSpan(bold, 0, 7, 0);
    mNotifications.add(new NotificationCompat.InboxStyle(
            new NotificationCompat.Builder(this).setContentTitle("24 new messages")
                    .setContentText("You have mail!").setSubText("test.hugo2@gmail.com")
                    .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Email"))
                    .setSmallIcon(R.drawable.stat_notify_email)).setSummaryText("+21 more").addLine(line1)
                            .addLine(line2).addLine(line3).build());

    mNotifications
            .add(new NotificationCompat.Builder(this).setContentTitle("Twitter").setContentText("New mentions")
                    .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Twitter"))
                    .setSmallIcon(R.drawable.twitter_icon).setNumber(15)
                    .setPriority(NotificationCompat.PRIORITY_LOW).build());

    for (int i = 0; i < mNotifications.size(); i++) {
        noMa.notify(NOTIFICATION_ID + i, mNotifications.get(i));
    }

    ProgressService.startProgressUpdater(this, uploadId, uploadWhen, 0);
}

From source file:com.rossier.shclechelles.service.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.//from ww w  .  j  a v  a 2s .co m
 */
private void sendNotification(String from, String message) {
    Gson gson = new GsonBuilder().create();
    String messageUTF8 = "";
    try {
        messageUTF8 = new String(message.getBytes(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    Log.i("MyGCMListenerService", message);
    Log.i("MyGCMListenerService", messageUTF8);
    if (messageUTF8 == "")
        return;
    MatchNotification match = gson.fromJson(messageUTF8, MatchNotification.class);
    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);

    int icon = R.drawable.ic_launcher_shc;
    long when = System.currentTimeMillis();
    // Notification notification = new Notification(icon, "Nouveaux rsultats", when);
    Notification notification = new Notification.Builder(this.getBaseContext())
            .setContentText("Nouveaux rsultats").setSmallIcon(icon).setWhen(when).build();

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.match_notif_layout);
    contentView.setTextViewText(R.id.notif_team_home, match.getTeam_home());
    contentView.setTextViewText(R.id.notif_team_away, match.getTeam_away());
    contentView.setTextViewText(R.id.notif_result_home, match.getResult_home() + "");
    contentView.setTextViewText(R.id.notif_result_away, match.getResult_away() + "");
    contentView.setTextViewText(R.id.notif_ligue, match.getLigue());
    contentView.setImageViewResource(R.id.notif_team_loc_logo, Utils.getLogo(match.getTeam_home()));
    contentView.setImageViewResource(R.id.notif_team_away_logo, Utils.getLogo(match.getTeam_away()));
    notification.contentView = contentView;

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

    //notification.flags |= Notification.FLAG_ONGOING_EVENT; //Do not clear the notification
    notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
    notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
    notification.defaults |= Notification.DEFAULT_SOUND; // Sound

    //get topics name
    String[] topic = from.split("/");

    mNotificationManager.notify(topic[2].hashCode(), notification);
}

From source file:com.near.chimerarevo.services.NewsService.java

private void createNotification() {
    if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean("notification_pref", true))
        return;//from  ww w . j  ava 2 s  .  co  m

    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
    bigStyle.bigText(getResources().getString(R.string.text_newposts_summary));
    bigStyle.setBigContentTitle(getResources().getString(R.string.app_name));

    NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
            .setContentIcon(R.drawable.ic_logo_cr_mini).setHintHideIcon(true);

    Notification n = new NotificationCompat.Builder(this)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setContentText(getResources().getString(R.string.text_newposts_summary))
            .setSmallIcon(R.drawable.ic_logo_cr_mini).setContentIntent(pIntent).extend(wearableExtender)
            .setAutoCancel(true).build();

    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("notification_vibrate_pref", true))
        n.defaults |= Notification.DEFAULT_VIBRATE;
    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("notification_sound_pref", true))
        n.defaults |= Notification.DEFAULT_SOUND;
    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("notification_light_pref", true)) {
        n.ledARGB = getLEDColor();
        n.ledOnMS = 300;
        n.ledOffMS = 1000;
        n.flags |= Notification.FLAG_SHOW_LIGHTS;
    }

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(0, n);
}

From source file:com.hoangsong.zumechat.gcm.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 *
 *//*from w  ww  .j  a v a 2 s  .  c om*/
private void sendNotification(CustomNotification customNotification) {

    String title = getString(R.string.app_name);
    long when = System.currentTimeMillis();

    NotificationCompat.Builder mBuilder = null;
    if (customNotification.getSound().equals("0") && customNotification.getVibrate().equals("0")) {
        mBuilder = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.mipmap.ic_launcher)
                //.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
                .setContentTitle(title).setContentText(customNotification.getMessage())// + " " + notificationsList.get(notificationsList.size()-1).getTime())
                //.setNumber(count)
                .setWhen(when).setAutoCancel(true)
        //.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationsList.get(notificationsList.size()-1).getMSG() + " " + notificationsList.get(notificationsList.size()-1).getTime()))
        //.setStyle(bigTextStyle)
        //.setTicker(message)
        ;
    } else if (customNotification.getSound().equals("1")) {
        mBuilder = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.mipmap.ic_launcher)
                //.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
                .setContentTitle(title).setContentText(customNotification.getMessage())// + " " + notificationsList.get(notificationsList.size()-1).getTime())
                //.setNumber(count)
                .setWhen(when).setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true)
        //.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationsList.get(notificationsList.size()-1).getMSG() + " " + notificationsList.get(notificationsList.size()-1).getTime()))
        //.setStyle(bigTextStyle)
        //.setTicker(message)
        ;
    } else if (customNotification.getVibrate().equals("1")) {
        mBuilder = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.mipmap.ic_launcher)
                //.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
                .setContentTitle(title).setContentText(customNotification.getMessage())// + " " + notificationsList.get(notificationsList.size()-1).getTime())
                //.setNumber(count)
                .setWhen(when).setDefaults(Notification.DEFAULT_VIBRATE).setAutoCancel(true)
        //.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationsList.get(notificationsList.size()-1).getMSG() + " " + notificationsList.get(notificationsList.size()-1).getTime()))
        //.setStyle(bigTextStyle)
        //.setTicker(message)
        ;
    } else {
        Log.e(Constants.TAG, MyGcmListenerService.class.getName() + " Exception: test all");
        mBuilder = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.mipmap.ic_launcher)
                //.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
                .setContentTitle(title).setContentText(customNotification.getMessage())// + " " + notificationsList.get(notificationsList.size()-1).getTime())
                //.setNumber(count)
                .setWhen(when).setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true)
        //.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationsList.get(notificationsList.size()-1).getMSG() + " " + notificationsList.get(notificationsList.size()-1).getTime()))
        //.setStyle(bigTextStyle)
        //.setTicker(message)
        ;
    }

    // Creates an explicit intent for an Activity in your app
    Intent notificationIntent = new Intent(getApplicationContext(), MainActivityPhone.class);
    if (customNotification != null) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("customNotification", customNotification);
        notificationIntent.putExtras(bundle);
    }

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivityPhone.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(notificationIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(customNotification.getId(),
            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(customNotification.getId(), mBuilder.build()); //Constants.NOTIFICATION_TYPE_ANNOUNCEMENT, mBuilder.build());
}

From source file:com.njlabs.amrita.aid.news.NewsUpdateService.java

private void getNews(final Boolean refresh, final List<NewsModel> oldArticles) {

    List<NewsModel> currentArticles;

    OkHttpClient client = new OkHttpClient.Builder().followRedirects(true).followSslRedirects(true).build();

    Request request = new Request.Builder().url("https://www.amrita.edu/campus/Coimbatore/news").build();

    try {//from   w  w  w .ja  v  a2s. co m
        Response response = client.newCall(request).execute();

        if (response.isSuccessful()) {
            String responseString = response.body().string();
            currentArticles = new ArrayList<>();
            if (refresh) {
                NewsModel.deleteAll();
            }

            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle("News Articles");

            Document doc = Jsoup.parse(responseString);
            Elements articles = doc.select("article");
            for (Element article : articles) {
                Element header = article.select(".flexslider").first();
                Element content = article.select(".group-blog-content").first();
                Element footer = article.select(".group-blog-footer").first();
                String imageUrl = header.select("ul > li > img").first().attr("src");
                String title = content.select(".field-name-title > div > div > h2").first().text();
                String url = "https://www.amrita.edu"
                        + footer.select(".field-name-node-link > div > div > a").first().attr("href");

                inboxStyle.addLine(title);
                currentArticles.add(new NewsModel(imageUrl, title, url));
            }

            ActiveAndroid.beginTransaction();
            try {
                for (NewsModel newsModel : currentArticles) {
                    newsModel.save();
                }
                ActiveAndroid.setTransactionSuccessful();
            } finally {
                ActiveAndroid.endTransaction();
            }

            Intent resultIntent = new Intent(mContext, NewsActivity.class);

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
            stackBuilder.addParentStack(NewsActivity.class);

            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            if (refresh && !currentArticles.get(0).getTitle().equals(oldArticles.get(0).getTitle())) {
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(NewsUpdateService.this)
                        .setSmallIcon(R.drawable.ic_stat_news)
                        .setLargeIcon(
                                BitmapFactory.decodeResource(mContext.getResources(), R.drawable.app_icon))
                        .setContentTitle("Latest News").setDefaults(Notification.DEFAULT_SOUND)
                        .setContentIntent(resultPendingIntent).setAutoCancel(true)
                        .setContentText(currentArticles.get(0).getTitle());

                if (allowNotification) {
                    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                            Context.NOTIFICATION_SERVICE);
                    mNotificationManager.notify(0, mBuilder.build());
                }

            }
            if (!refresh) {
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);

                mBuilder.setContentTitle("New Posts");
                mBuilder.setContentText("Latest News Updated");
                mBuilder.setTicker("Latest News Updated");
                mBuilder.setSmallIcon(R.drawable.ic_stat_news);
                mBuilder.setLargeIcon(
                        BitmapFactory.decodeResource(mContext.getResources(), R.drawable.app_icon));
                mBuilder.setContentIntent(resultPendingIntent);
                mBuilder.setNumber(currentArticles.size());
                mBuilder.setAutoCancel(true);
                mBuilder.setStyle(inboxStyle);

                if (allowNotification) {
                    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                            Context.NOTIFICATION_SERVICE);
                    mNotificationManager.notify(0, mBuilder.build());
                }

            }
        }
    } catch (IOException ignored) {
        FirebaseCrash.report(ignored);
    }

}

From source file:com.oliversride.wordryo.Utils.java

public static void postNotification(Context context, Intent intent, String title, String body, int id) {
    /* nextRandomInt: per this link
       http://stackoverflow.com/questions/10561419/scheduling-more-than-one-pendingintent-to-same-activity-using-alarmmanager
       one way to avoid getting the same PendingIntent for similar
       Intents is to send a different second param each time,
       though the docs say that param's ignored.
    *///from  w  w  w. j ava 2 s.c o  m
    PendingIntent pi = null == intent ? null
            : PendingIntent.getActivity(context, Utils.nextRandomInt(), intent, PendingIntent.FLAG_ONE_SHOT);

    int defaults = Notification.FLAG_AUTO_CANCEL;
    if (CommonPrefs.getSoundNotify(context)) {
        defaults |= Notification.DEFAULT_SOUND;
    }
    if (CommonPrefs.getVibrateNotify(context)) {
        defaults |= Notification.DEFAULT_VIBRATE;
    }

    Notification notification = new NotificationCompat.Builder(context).setContentIntent(pi)
            .setSmallIcon(R.drawable.notify)
            //.setTicker(body)
            //.setWhen(time)
            .setAutoCancel(true).setDefaults(defaults).setContentTitle(title).setContentText(body).build();

    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(id, notification);
}

From source file:com.automated.taxinow.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*from w  ww .  ja  v a2s. c  om*/
private void generateNotification(Context context, String message) {
    // System.out.println("this is message " + message);
    // System.out.println("NOTIFICATION RECEIVED!!!!!!!!!!!!!!" + message);
    int icon = R.drawable.ic_launcher;
    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, MainDrawerActivity.class);
    notificationIntent.putExtra("fromNotification", "notification");
    // 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,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    System.out.println("notification====>" + message);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    // notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.ledARGB = 0x00000000;
    notification.ledOnMS = 0;
    notification.ledOffMS = 0;
    notificationManager.notify(0, notification);
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(
            PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
            "WakeLock");
    wakeLock.acquire();
    wakeLock.release();
}