Example usage for android.app Notification FLAG_SHOW_LIGHTS

List of usage examples for android.app Notification FLAG_SHOW_LIGHTS

Introduction

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

Prototype

int FLAG_SHOW_LIGHTS

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

Click Source Link

Document

Bit to be bitwise-ored into the #flags field that should be set if you want the LED on for this notification.

Usage

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  .ja  v a  2  s  .  c o  m
    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:com.hollandhaptics.babyapp.BabyService.java

/**
 * @brief Lets the LED indicator flash./*from   w  ww.  j  a v  a2s.  c  om*/
 */
private void FlashLED() {
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notif = new Notification();
    notif.ledARGB = 0xFF0000ff;
    notif.flags = Notification.FLAG_SHOW_LIGHTS;
    notif.ledOnMS = 100;
    notif.ledOffMS = 900;
    nm.notify(0, notif);
}

From source file:com.twofours.surespot.services.SurespotGcmListenerService.java

private void generateNotification(Context context, String type, String from, String to, String title,
        String message, String tag, int id) {
    SurespotLog.d(TAG, "generateNotification");
    // get shared prefs
    SharedPreferences pm = context.getSharedPreferences(to, Context.MODE_PRIVATE);
    if (!pm.getBoolean("pref_notifications_enabled", true)) {
        return;/* w w w .java  2 s . co m*/
    }

    int icon = R.drawable.surespot_logo;

    // need to use same builder for only alert once to work:
    // http://stackoverflow.com/questions/6406730/updating-an-ongoing-notification-quietly
    mBuilder.setSmallIcon(icon).setContentTitle(title).setAutoCancel(true).setOnlyAlertOnce(false)
            .setContentText(message);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

    Intent mainIntent = null;
    mainIntent = new Intent(context, MainActivity.class);
    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mainIntent.putExtra(SurespotConstants.ExtraNames.MESSAGE_TO, to);
    mainIntent.putExtra(SurespotConstants.ExtraNames.MESSAGE_FROM, from);
    mainIntent.putExtra(SurespotConstants.ExtraNames.NOTIFICATION_TYPE, type);

    stackBuilder.addNextIntent(mainIntent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent((int) new Date().getTime(),
            PendingIntent.FLAG_CANCEL_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);
    int defaults = 0;

    boolean showLights = pm.getBoolean("pref_notifications_led", true);
    boolean makeSound = pm.getBoolean("pref_notifications_sound", true);
    boolean vibrate = pm.getBoolean("pref_notifications_vibration", true);
    int color = pm.getInt("pref_notification_color", getResources().getColor(R.color.surespotBlue));

    if (showLights) {
        SurespotLog.v(TAG, "showing notification led");
        mBuilder.setLights(color, 500, 5000);
        defaults |= Notification.FLAG_SHOW_LIGHTS; // shouldn't need this - setLights does it.  Just to make sure though...
    } else {
        mBuilder.setLights(color, 0, 0);
    }

    if (makeSound) {
        SurespotLog.v(TAG, "making notification sound");
        defaults |= Notification.DEFAULT_SOUND;
    }

    if (vibrate) {
        SurespotLog.v(TAG, "vibrating notification");
        defaults |= Notification.DEFAULT_VIBRATE;
    }

    mBuilder.setDefaults(defaults);
    mNotificationManager.notify(tag, id, mBuilder.build());
}

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);// w  ww  .  j av a2  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:org.numixproject.hermes.irc.IRCService.java

/**
 * Update notification and vibrate and/or flash a LED light if needed
 *
 * @param text       The ticker text to display
 * @param contentText       The text to display in the notification dropdown
 * @param vibrate True if the device should vibrate, false otherwise
 * @param sound True if the device should make sound, false otherwise
 * @param light True if the device should flash a LED light, false otherwise
 *//*  www.j a va2s.  com*/
private void updateNotification(String text, String contentText, boolean vibrate, boolean sound,
        boolean light) {
    Intent disconnect = new Intent("disconnect_all");
    PendingIntent pendingIntentDisconnect = PendingIntent.getBroadcast(this, 0, disconnect,
            PendingIntent.FLAG_CANCEL_CURRENT);

    if (foreground) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentText(text);
        builder.setSmallIcon(R.drawable.ic_stat_hermes2);
        builder.setWhen(System.currentTimeMillis());
        builder.addAction(R.drawable.ic_action_ic_close_24px, "DISCONNECT ALL", pendingIntentDisconnect);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder.setColor(Color.parseColor("#0097A7"));
        }

        Intent notifyIntent = new Intent(this, MainActivity.class);
        notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);

        if (contentText == null) {
            if (newMentions >= 1) {
                StringBuilder sb = new StringBuilder();
                for (Conversation conv : mentions.values()) {
                    sb.append(conv.getName() + " (" + conv.getNewMentions() + "), ");
                }
                contentText = getString(R.string.notification_mentions, sb.substring(0, sb.length() - 2));
            } else if (!connectedServerTitles.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                for (String title : connectedServerTitles) {
                    sb.append(title + ", ");
                }
                contentText = getString(R.string.notification_connected, sb.substring(0, sb.length() - 2));
            } else {
                contentText = getString(R.string.notification_not_connected);
            }
        }

        builder.setContentIntent(contentIntent).setWhen(System.currentTimeMillis())
                .setContentTitle(getText(R.string.app_name)).setContentText(contentText);
        Notification notification = builder.build();

        if (vibrate) {
            notification.defaults |= Notification.DEFAULT_VIBRATE;
        }

        if (sound) {
            notification.defaults |= Notification.DEFAULT_SOUND;
        }

        if (light) {
            notification.ledARGB = NOTIFICATION_LED_COLOR;
            notification.ledOnMS = NOTIFICATION_LED_ON_MS;
            notification.ledOffMS = NOTIFICATION_LED_OFF_MS;
            notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        }

        notification.number = newMentions;

        notificationManager.notify(FOREGROUND_NOTIFICATION, notification);
    }
}

From source file:org.sipdroid.sipua.ui.Receiver.java

public static void onText(int type, String text, int mInCallResId, long base) {
    if (mSipdroidEngine != null && type == REGISTER_NOTIFICATION + mSipdroidEngine.pref) {
        cache_text = text;/* ww w. j  a v a 2s.  com*/
        cache_res = mInCallResId;
    }
    if (type >= REGISTER_NOTIFICATION && mInCallResId == R.drawable.sym_presence_available
            && !PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getBoolean(
                    org.sipdroid.sipua.ui.Settings.PREF_REGISTRATION,
                    org.sipdroid.sipua.ui.Settings.DEFAULT_REGISTRATION))
        text = null;
    NotificationManager mNotificationMgr = (NotificationManager) mContext
            .getSystemService(Context.NOTIFICATION_SERVICE);
    if (text != null) {
        Notification notification = new Notification();
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);

        notification.icon = mInCallResId;
        if (type == MISSED_CALL_NOTIFICATION) {
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            //TODO
            /*notification.setLatestEventInfo(mContext, text, mContext.getString(R.string.app_name),
                  PendingIntent.getActivity(mContext, 0, createCallLogIntent(), 0));
            if (PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getBoolean(org.sipdroid.sipua.ui.Settings.PREF_NOTIFY, org.sipdroid.sipua.ui.Settings.DEFAULT_NOTIFY)) {
               notification.flags |= Notification.FLAG_SHOW_LIGHTS;
               notification.ledARGB = 0xff0000ff;  blue 
               notification.ledOnMS = 125;
               notification.ledOffMS = 2875;
            }*/
        } else {
            switch (type) {
            case MWI_NOTIFICATION:
                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                notification.contentIntent = PendingIntent.getActivity(mContext, 0, createMWIIntent(), 0);
                notification.flags |= Notification.FLAG_SHOW_LIGHTS;
                notification.ledARGB = 0xff00ff00; /* green */
                notification.ledOnMS = 125;
                notification.ledOffMS = 2875;
                break;
            case AUTO_ANSWER_NOTIFICATION:
                notification.contentIntent = PendingIntent.getActivity(mContext, 0,
                        createIntent(AutoAnswer.class), 0);
                break;
            default:
                if (type >= REGISTER_NOTIFICATION && mSipdroidEngine != null
                        && type != REGISTER_NOTIFICATION + mSipdroidEngine.pref
                        && mInCallResId == R.drawable.sym_presence_available) {
                    int key = type - REGISTER_NOTIFICATION;
                    Intent in = createIntent(ChangeAccount.class);
                    in.setAction(Long.toString(type));
                    in.putExtra(ChangeAccount.ChangeAccountKey, key);
                    in.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    notification.contentIntent = PendingIntent.getActivity(mContext, key, in,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                } else
                    notification.contentIntent = PendingIntent.getActivity(mContext, 0,
                            createIntent(Sipdroid.class), 0);
                if (mInCallResId == R.drawable.sym_presence_away) {
                    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
                    notification.ledARGB = 0xffff0000; /* red */
                    notification.ledOnMS = 125;
                    notification.ledOffMS = 2875;
                }
                notification.flags |= Notification.FLAG_ONGOING_EVENT;
                break;
            }

            RemoteViews contentView = new RemoteViews(mContext.getPackageName(),
                    R.layout.ongoing_call_notification);
            contentView.setImageViewResource(R.id.icon, notification.icon);
            if (base != 0) {
                contentView.setChronometer(R.id.text1, base, text + " (%s)", true);
            } else if (type >= REGISTER_NOTIFICATION) {
                if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(
                        org.sipdroid.sipua.ui.Settings.PREF_POS, org.sipdroid.sipua.ui.Settings.DEFAULT_POS))
                    contentView.setTextViewText(R.id.text2,
                            text + "/" + mContext.getString(R.string.settings_pos3));
                else
                    contentView.setTextViewText(R.id.text2, text);
                if (mSipdroidEngine != null)
                    contentView.setTextViewText(R.id.text1,
                            mSipdroidEngine.user_profiles[type - REGISTER_NOTIFICATION].username + "@"
                                    + mSipdroidEngine.user_profiles[type - REGISTER_NOTIFICATION].realm_orig);
            } else
                contentView.setTextViewText(R.id.text1, text);
            notification.contentView = contentView;
        }
        mNotificationMgr.notify(type, notification);
    } else {
        mNotificationMgr.cancel(type);
    }
    if (type != AUTO_ANSWER_NOTIFICATION)
        updateAutoAnswer();
    if (mSipdroidEngine != null && type >= REGISTER_NOTIFICATION
            && type != REGISTER_NOTIFICATION + mSipdroidEngine.pref)
        onText(REGISTER_NOTIFICATION + mSipdroidEngine.pref, cache_text, cache_res, 0);
}

From source file:io.coldstart.android.GCMIntentService.java

private void SendCombinedNotification(String EventCount) {
    Notification notification = new Notification(R.drawable.ic_stat_alert, EventCount + " new SNMP Traps!",
            System.currentTimeMillis());
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;

    notification.ledARGB = 0xffff0000;/*w  w w  . j a  v  a2  s  .  com*/

    notification.ledOnMS = 300;
    notification.ledOffMS = 1000;

    notification.defaults |= Notification.DEFAULT_SOUND;

    Context context = getApplicationContext();
    Intent notificationIntent = new Intent(this, TrapListActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("forceRefresh", true);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, EventCount + " SNMP Traps", "Click to launch ColdStart.io",
            contentIntent);
    mNM.notify(43523, notification);//NotificationID++ 
}

From source file:im.neon.util.NotificationUtils.java

/**
 * Build a message notification.//  w  w w. ja  v a2 s  .  c om
 * @param context the context
 * @param from the sender
 * @param matrixId the user account id;
 * @param displayMatrixId true to display the matrix id
 * @param largeIcon the notification icon
 * @param unseenNotifiedRoomsCount the number of notified rooms
 * @param body the message body
 * @param roomId the room id
 * @param roomName the room name
 * @param shouldPlaySound true when the notification as sound.
 * @param isInvitationEvent true if it is an invitation notification
 * @return the notification
 */
public static Notification buildMessageNotification(Context context, String from, String matrixId,
        boolean displayMatrixId, Bitmap largeIcon, int unseenNotifiedRoomsCount, String body, String roomId,
        String roomName, boolean shouldPlaySound, boolean isInvitationEvent) {

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setWhen(System.currentTimeMillis());

    if (!TextUtils.isEmpty(from)) {
        // don't display the room name for 1:1 room notifications.
        if (!TextUtils.isEmpty(roomName) && !roomName.equals(from)) {
            builder.setContentTitle(from + " (" + roomName + ")");
        } else {
            builder.setContentTitle(from);
        }
    } else {
        builder.setContentTitle(roomName);
    }

    builder.setContentText(body);
    builder.setAutoCancel(true);
    builder.setSmallIcon(R.drawable.message_notification_transparent);

    if (null != largeIcon) {
        largeIcon = createSquareBitmap(largeIcon);

        // add a bubble in the top right
        if (0 != unseenNotifiedRoomsCount) {
            try {
                android.graphics.Bitmap.Config bitmapConfig = largeIcon.getConfig();

                // set default bitmap config if none
                if (bitmapConfig == null) {
                    bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
                }

                // setLargeIcon must used a 64 * 64 pixels bitmap
                // rescale to have the same text UI.
                float densityScale = context.getResources().getDisplayMetrics().density;
                int side = (int) (64 * densityScale);

                Bitmap bitmapCopy = Bitmap.createBitmap(side, side, bitmapConfig);
                Canvas canvas = new Canvas(bitmapCopy);

                // resize the bitmap to fill in size
                int bitmapWidth = largeIcon.getWidth();
                int bitmapHeight = largeIcon.getHeight();

                float scale = Math.min((float) canvas.getWidth() / (float) bitmapWidth,
                        (float) canvas.getHeight() / (float) bitmapHeight);

                int scaledWidth = (int) (bitmapWidth * scale);
                int scaledHeight = (int) (bitmapHeight * scale);

                Bitmap rescaledBitmap = Bitmap.createScaledBitmap(largeIcon, scaledWidth, scaledHeight, true);
                canvas.drawBitmap(rescaledBitmap, (side - scaledWidth) / 2, (side - scaledHeight) / 2, null);

                String text = "" + unseenNotifiedRoomsCount;

                // prepare the text drawing
                Paint textPaint = new Paint();
                textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
                textPaint.setColor(Color.WHITE);
                textPaint.setTextSize(10 * densityScale);

                // get its size
                Rect textBounds = new Rect();

                if (-1 == mUnreadBubbleWidth) {
                    textPaint.getTextBounds("99", 0, 2, textBounds);
                    mUnreadBubbleWidth = textBounds.width();
                }

                textPaint.getTextBounds(text, 0, text.length(), textBounds);

                // draw a red circle
                int radius = mUnreadBubbleWidth;
                Paint paint = new Paint();
                paint.setStyle(Paint.Style.FILL);
                paint.setColor(Color.RED);
                canvas.drawCircle(canvas.getWidth() - radius, radius, radius, paint);

                // draw the text
                canvas.drawText(text,
                        canvas.getWidth() - textBounds.width() - (radius - (textBounds.width() / 2)),
                        -textBounds.top + (radius - (-textBounds.top / 2)), textPaint);

                // get the new bitmap
                largeIcon = bitmapCopy;
            } catch (Exception e) {
                Log.e(LOG_TAG, "## buildMessageNotification(): Exception Msg=" + e.getMessage());
            }
        }

        builder.setLargeIcon(largeIcon);
    }

    String name = ": ";
    if (!TextUtils.isEmpty(roomName)) {
        name = " (" + roomName + "): ";
    }

    if (displayMatrixId) {
        from = "[" + matrixId + "]\n" + from;
    }

    builder.setTicker(from + name + body);

    TaskStackBuilder stackBuilder;
    Intent intent;

    intent = new Intent(context, VectorRoomActivity.class);
    intent.putExtra(VectorRoomActivity.EXTRA_ROOM_ID, roomId);

    if (null != matrixId) {
        intent.putExtra(VectorRoomActivity.EXTRA_MATRIX_ID, matrixId);
    }

    stackBuilder = TaskStackBuilder.create(context).addParentStack(VectorRoomActivity.class)
            .addNextIntent(intent);

    // android 4.3 issue
    // use a generator for the private requestCode.
    // When using 0, the intent is not created/launched when the user taps on the notification.
    //
    PendingIntent pendingIntent = stackBuilder.getPendingIntent((new Random()).nextInt(1000),
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);

    // display the message with more than 1 lines when the device supports it
    NotificationCompat.BigTextStyle textStyle = new NotificationCompat.BigTextStyle();
    textStyle.bigText(from + ":" + body);
    builder.setStyle(textStyle);

    // do not offer to quick respond if the user did not dismiss the previous one
    if (!LockScreenActivity.isDisplayingALockScreenActivity()) {
        if (!isInvitationEvent) {
            // offer to type a quick answer (i.e. without launching the application)
            Intent quickReplyIntent = new Intent(context, LockScreenActivity.class);
            quickReplyIntent.putExtra(LockScreenActivity.EXTRA_ROOM_ID, roomId);
            quickReplyIntent.putExtra(LockScreenActivity.EXTRA_SENDER_NAME, from);
            quickReplyIntent.putExtra(LockScreenActivity.EXTRA_MESSAGE_BODY, body);

            if (null != matrixId) {
                quickReplyIntent.putExtra(LockScreenActivity.EXTRA_MATRIX_ID, matrixId);
            }

            // the action must be unique else the parameters are ignored
            quickReplyIntent.setAction(QUICK_LAUNCH_ACTION + ((int) (System.currentTimeMillis())));
            PendingIntent pIntent = PendingIntent.getActivity(context, 0, quickReplyIntent, 0);
            builder.addAction(R.drawable.vector_notification_quick_reply,
                    context.getString(R.string.action_quick_reply), pIntent);
        } else {
            {
                // offer to type a quick reject button
                Intent leaveIntent = new Intent(context, JoinScreenActivity.class);
                leaveIntent.putExtra(JoinScreenActivity.EXTRA_ROOM_ID, roomId);
                leaveIntent.putExtra(JoinScreenActivity.EXTRA_MATRIX_ID, matrixId);
                leaveIntent.putExtra(JoinScreenActivity.EXTRA_REJECT, true);

                // the action must be unique else the parameters are ignored
                leaveIntent.setAction(QUICK_LAUNCH_ACTION + ((int) (System.currentTimeMillis())));
                PendingIntent pIntent = PendingIntent.getActivity(context, 0, leaveIntent, 0);
                builder.addAction(R.drawable.vector_notification_reject_invitation,
                        context.getString(R.string.reject), pIntent);
            }

            {
                // offer to type a quick accept button
                Intent acceptIntent = new Intent(context, JoinScreenActivity.class);
                acceptIntent.putExtra(JoinScreenActivity.EXTRA_ROOM_ID, roomId);
                acceptIntent.putExtra(JoinScreenActivity.EXTRA_MATRIX_ID, matrixId);
                acceptIntent.putExtra(JoinScreenActivity.EXTRA_JOIN, true);

                // the action must be unique else the parameters are ignored
                acceptIntent.setAction(QUICK_LAUNCH_ACTION + ((int) (System.currentTimeMillis())));
                PendingIntent pIntent = PendingIntent.getActivity(context, 0, acceptIntent, 0);
                builder.addAction(R.drawable.vector_notification_accept_invitation,
                        context.getString(R.string.join), pIntent);
            }
        }

        // Build the pending intent for when the notification is clicked
        Intent roomIntentTap;
        if (isInvitationEvent) {
            // for invitation the room preview must be displayed
            roomIntentTap = CommonActivityUtils.buildIntentPreviewRoom(matrixId, roomId, context,
                    VectorFakeRoomPreviewActivity.class);
        } else {
            roomIntentTap = new Intent(context, VectorRoomActivity.class);
            roomIntentTap.putExtra(VectorRoomActivity.EXTRA_ROOM_ID, roomId);
        }
        // the action must be unique else the parameters are ignored
        roomIntentTap.setAction(TAP_TO_VIEW_ACTION + ((int) (System.currentTimeMillis())));

        // Recreate the back stack
        TaskStackBuilder stackBuilderTap = TaskStackBuilder.create(context)
                .addParentStack(VectorRoomActivity.class).addNextIntent(roomIntentTap);

        builder.addAction(R.drawable.vector_notification_open, context.getString(R.string.action_open),
                stackBuilderTap.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
    }

    //extendForCar(context, builder, roomId, roomName, from, body);

    Notification n = builder.build();
    n.flags |= Notification.FLAG_SHOW_LIGHTS;
    n.defaults |= Notification.DEFAULT_LIGHTS;

    if (shouldPlaySound) {
        n.defaults |= Notification.DEFAULT_SOUND;
    }

    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        // some devices crash if this field is not set
        // even if it is deprecated

        // setLatestEventInfo() is deprecated on Android M, so we try to use
        // reflection at runtime, to avoid compiler error: "Cannot resolve method.."
        try {
            Method deprecatedMethod = n.getClass().getMethod("setLatestEventInfo", Context.class,
                    CharSequence.class, CharSequence.class, PendingIntent.class);
            deprecatedMethod.invoke(n, context, from, body, pendingIntent);
        } catch (Exception ex) {
            Log.e(LOG_TAG,
                    "## buildMessageNotification(): Exception - setLatestEventInfo() Msg=" + ex.getMessage());
        }
    }

    return n;
}

From source file:indrora.atomic.irc.IRCService.java

/**
 * Update notification and vibrate and/or flash a LED light if needed
 *
 * @param text       The ticker text to display
 * @param contentText       The text to display in the notification dropdown
 *                          If null, this makes the notification update to be the connection status.
 * @param vibrate True if the device should vibrate, false otherwise
 * @param sound True if the device should make sound, false otherwise
 * @param light True if the device should flash a LED light, false otherwise
 *///from  w w  w.ja  va2 s.  co m
private void updateNotification(String text, String contentText, boolean vibrate, boolean sound,
        boolean light) {
    if (foreground) {
        // I give up. Android changed how this works -- Hope it never goes away.
        notification = new Notification(R.drawable.ic_service_icon, text, System.currentTimeMillis());

        Intent notifyIntent = new Intent(this, ServersActivity.class);
        //notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //conetntText is null when you have nothing to display; by default, if you supply any, it will
        //use whatever is handed as the body of the notification.
        // If you hand it null, you are given a status line that describes what is going on.
        if (contentText == null) {
            if (newMentions >= 1) {
                StringBuilder sb = new StringBuilder();
                for (Conversation conv : mentions.values()) {
                    sb.append(conv.getName() + " (" + conv.getNewMentions() + "), ");
                }
                contentText = getString(R.string.notification_mentions, sb.substring(0, sb.length() - 2));

                // We're going to work through the mentions keys. The first half is
                // the server ID, the other half
                // is the channel that the mention belongs to.

                int ServerID = -1;
                String Convo = "";
                for (String convID : mentions.keySet()) {
                    ServerID = Integer.parseInt(convID.substring(0, convID.indexOf(':')));
                    Convo = convID.substring(convID.indexOf(':') + 1);
                }

                Log.d("IRCService", "Jump target is '" + Convo + "'");
                notifyIntent.setClass(this, ConversationActivity.class);
                notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                notifyIntent.putExtra("serverId", ServerID);
                notifyIntent.putExtra(ConversationActivity.EXTRA_TARGET, "" + Convo);

            } else {
                if (!connectedServerTitles.isEmpty()) {
                    StringBuilder sb = new StringBuilder();
                    for (String title : connectedServerTitles) {
                        sb.append(title + ", ");
                    }
                    contentText = getString(R.string.notification_connected, sb.substring(0, sb.length() - 2));
                } else {
                    contentText = getString(R.string.notification_not_connected);
                }
            }
        }

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notifyIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(this, getText(R.string.app_name), contentText, contentIntent);

        // We only want to vibrate if it's been $ARBITRARY_AMOUNT_OF_TIME
        // since we last buzzed.

        vibrate = vibrate && (System.currentTimeMillis() - lastVibrationTime > 2000);

        if (vibrate) {
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            lastVibrationTime = System.currentTimeMillis();
        }

        if (sound) {
            // buzz the user with an audible sound.
            notification.sound = settings.getHighlightSoundLocation();

        }

        if (light) {
            notification.ledARGB = NOTIFICATION_LED_COLOR;
            notification.ledOnMS = NOTIFICATION_LED_ON_MS;
            notification.ledOffMS = NOTIFICATION_LED_OFF_MS;
            notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        }

        notification.number = newMentions;

        notificationManager.notify(FOREGROUND_NOTIFICATION, notification);
    }
}

From source file:co.beem.project.beem.FacebookTextService.java

/**
 * Show a notification using the preference of the user.
 * //from  w ww.j  a v a 2  s . co m
 * @param id
 *            the id of the notification.
 * @param notif
 *            the notification to show
 */
public void sendNotification(int id, Notification notif) {
    if (mSettings.getBoolean(FacebookTextApplication.NOTIFICATION_VIBRATE_KEY, true))
        notif.defaults |= Notification.DEFAULT_VIBRATE;
    notif.ledARGB = 0xff0000ff; // Blue color
    notif.ledOnMS = 1000;
    notif.ledOffMS = 1000;
    notif.flags |= Notification.FLAG_SHOW_LIGHTS;
    String ringtoneStr = mSettings.getString(FacebookTextApplication.NOTIFICATION_SOUND_KEY,
            Settings.System.DEFAULT_NOTIFICATION_URI.toString());
    notif.sound = Uri.parse(ringtoneStr);
    if (mSettings.getBoolean("notifications_new_message", true))
        mNotificationManager.notify(id, notif);
}