Example usage for android.app Notification CATEGORY_MESSAGE

List of usage examples for android.app Notification CATEGORY_MESSAGE

Introduction

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

Prototype

String CATEGORY_MESSAGE

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

Click Source Link

Document

Notification category: incoming direct message (SMS, instant message, etc.).

Usage

From source file:org.linphone.compatibility.ApiTwentyOnePlus.java

public static Notification createMessageNotification(Context context, int msgCount, String msgSender,
        String msg, Bitmap contactIcon, PendingIntent intent) {
    String title;//  w  ww .  j a v  a2 s .c  o  m
    if (msgCount == 1) {
        title = msgSender;
    } else {
        title = context.getString(R.string.unread_messages).replace("%i", String.valueOf(msgCount));
    }

    Notification notif = new NotificationCompat.Builder(context).setContentTitle(title).setContentText(msg)
            .setSmallIcon(R.drawable.chat_icon_over).setAutoCancel(true).setContentIntent(intent)
            .setDefaults(Notification.DEFAULT_ALL).setLargeIcon(contactIcon)
            .setCategory(Notification.CATEGORY_MESSAGE).setVisibility(Notification.VISIBILITY_PRIVATE)
            .setPriority(Notification.PRIORITY_HIGH).build();

    return notif;
}

From source file:org.linphone.compatibility.ApiTwentyOnePlus.java

public static Notification createSimpleNotification(Context context, String title, String text,
        PendingIntent intent) {/*from   ww w.  j  ava  2 s  .  c o m*/
    Notification notif = new NotificationCompat.Builder(context).setContentTitle(title).setContentText(text)
            .setSmallIcon(R.drawable.logo_linphone_57x57).setAutoCancel(true).setContentIntent(intent)
            .setDefaults(Notification.DEFAULT_ALL).setCategory(Notification.CATEGORY_MESSAGE)
            .setVisibility(Notification.VISIBILITY_PRIVATE).setPriority(Notification.PRIORITY_HIGH).build();

    return notif;
}

From source file:net.olejon.mdapp.MessageIntentService.java

@Override
protected void onHandleIntent(Intent intent) {
    final Context mContext = this;

    final MyTools mTools = new MyTools(mContext);

    RequestQueue requestQueue = Volley.newRequestQueue(mContext);

    int projectVersionCode = mTools.getProjectVersionCode();

    String device = mTools.getDevice();

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
            getString(R.string.project_website_uri) + "api/1/message/?version_code=" + projectVersionCode
                    + "&device=" + device,
            new Response.Listener<JSONObject>() {
                @SuppressLint("InlinedApi")
                @Override/*ww  w  .  j  ava2 s . com*/
                public void onResponse(JSONObject response) {
                    try {
                        final long id = response.getLong("id");
                        final String title = response.getString("title");
                        final String message = response.getString("message");
                        final String bigMessage = response.getString("big_message");
                        final String button = response.getString("button");
                        final String uri = response.getString("uri");

                        final long lastId = mTools.getSharedPreferencesLong("MESSAGE_LAST_ID");

                        mTools.setSharedPreferencesLong("MESSAGE_LAST_ID", id);

                        if (lastId != 0 && id != lastId) {
                            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                                    R.drawable.ic_launcher);

                            NotificationManagerCompat notificationManager = NotificationManagerCompat
                                    .from(mContext);
                            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                                    mContext);

                            notificationBuilder.setWhen(mTools.getCurrentTime()).setAutoCancel(true)
                                    .setPriority(Notification.PRIORITY_HIGH)
                                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                                    .setCategory(Notification.CATEGORY_MESSAGE).setLargeIcon(bitmap)
                                    .setSmallIcon(R.drawable.ic_local_hospital_white_24dp)
                                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                                    .setLights(Color.BLUE, 1000, 2000).setTicker(message).setContentTitle(title)
                                    .setContentText(message)
                                    .setStyle(new NotificationCompat.BigTextStyle().bigText(bigMessage));

                            if (!uri.equals("")) {
                                Intent launchIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                                PendingIntent launchPendingIntent = PendingIntent.getActivity(mContext, 0,
                                        launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                                notificationBuilder.setContentIntent(launchPendingIntent).addAction(
                                        R.drawable.ic_local_hospital_white_24dp, button, launchPendingIntent);
                            }

                            notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
                        }
                    } catch (Exception e) {
                        Log.e("MessageIntentService", Log.getStackTraceString(e));
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("MessageIntentService", error.toString());
                }
            });

    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    requestQueue.add(jsonObjectRequest);
}

From source file:com.example.android.lnotifications.HeadsUpNotificationFragment.java

/**
 * Creates a new notification depending on the argument.
 *
 * @param makeHeadsUpNotification A boolean value to indicating whether a notification will be
 *                                created as a heads-up notification or not.
 *                                <ul>
 *                                <li>true : Creates a heads-up notification.</li>
 *                                <li>false : Creates a non-heads-up notification.</li>
 *                                </ul>
 *
 * @return A Notification instance.//  w ww  . ja v  a  2 s  .  c  om
 */
private Notification createNotification(boolean makeHeadsUpNotification) {
    Notification.Builder notificationBuilder = new Notification.Builder(getActivity())
            .setSmallIcon(R.drawable.ic_launcher_notification).setPriority(Notification.PRIORITY_DEFAULT)
            .setCategory(Notification.CATEGORY_MESSAGE).setContentTitle("Sample Notification")
            .setContentText("This is a normal notification.");
    if (makeHeadsUpNotification) {
        Intent push = new Intent();
        push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        push.setClass(getActivity(), LNotificationActivity.class);

        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(getActivity(), 0, push,
                PendingIntent.FLAG_CANCEL_CURRENT);
        notificationBuilder.setContentText("Heads-Up Notification on Android L or above.")
                .setFullScreenIntent(fullScreenPendingIntent, true);
    }
    return notificationBuilder.build();
}

From source file:net.olejon.mdapp.NotificationsFromSlvIntentService.java

@Override
protected void onHandleIntent(Intent intent) {
    final Context mContext = this;

    final MyTools mTools = new MyTools(mContext);

    if (mTools.getDefaultSharedPreferencesBoolean("NOTIFICATIONS_FROM_SLV_NOTIFY")
            && mTools.isDeviceConnected()) {
        RequestQueue requestQueue = Volley.newRequestQueue(mContext);

        int projectVersionCode = mTools.getProjectVersionCode();

        String device = mTools.getDevice();

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
                getString(R.string.project_website_uri) + "api/1/notifications-from-slv/?first&version_code="
                        + projectVersionCode + "&device=" + device,
                new Response.Listener<JSONArray>() {
                    @SuppressLint("InlinedApi")
                    @Override/*w  w w.j ava  2 s.  c om*/
                    public void onResponse(JSONArray response) {
                        try {
                            final JSONObject notifications = response.getJSONObject(0);

                            final String title = notifications.getString("title");
                            final String message = notifications.getString("message");

                            final String lastTitle = mTools
                                    .getSharedPreferencesString("NOTIFICATIONS_FROM_SLV_LAST_TITLE");

                            if (!title.equals(lastTitle)) {
                                if (!lastTitle.equals("")) {
                                    Intent launchIntent = new Intent(mContext,
                                            NotificationsFromSlvActivity.class);
                                    PendingIntent launchPendingIntent = PendingIntent.getActivity(mContext, 0,
                                            launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                                            R.drawable.ic_launcher);

                                    NotificationManagerCompat notificationManager = NotificationManagerCompat
                                            .from(mContext);
                                    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                                            mContext);

                                    notificationBuilder.setWhen(mTools.getCurrentTime())
                                            .setPriority(Notification.PRIORITY_HIGH)
                                            .setVisibility(Notification.VISIBILITY_PUBLIC)
                                            .setCategory(Notification.CATEGORY_MESSAGE).setLargeIcon(bitmap)
                                            .setSmallIcon(R.drawable.ic_local_hospital_white_24dp)
                                            .setSound(RingtoneManager
                                                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                                            .setLights(Color.BLUE, 1000, 2000).setTicker(title)
                                            .setContentTitle(title).setContentText(message)
                                            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                                            .setContentIntent(launchPendingIntent)
                                            .addAction(R.drawable.ic_notifications_white_24dp,
                                                    getString(
                                                            R.string.service_notifications_from_slv_read_more),
                                                    launchPendingIntent);

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

                                mTools.setSharedPreferencesString("NOTIFICATIONS_FROM_SLV_LAST_TITLE", title);
                            }
                        } catch (Exception e) {
                            Log.e("NotificationsFromSlv", Log.getStackTraceString(e));
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("NotificationsFromSlv", error.toString());
                    }
                });

        jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        requestQueue.add(jsonArrayRequest);
    }
}

From source file:rp.soi.dmsd.notextingwhilewalking.DetectedActivitiesIntentService.java

public void createNotification(boolean makeHeadsUpNotification) {

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_priority_high_black_24dp).setPriority(Notification.PRIORITY_DEFAULT)
            .setCategory(Notification.CATEGORY_MESSAGE).setContentTitle("No-Texting-While-Walking")
            .setContentText("It is hazardous to be texting while walking.");

    if (makeHeadsUpNotification) {

        Intent push = new Intent(getApplication(), MainActivity.class);
        push.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        //push.setClass(this, MainActivity.class);

        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, push,
                PendingIntent.FLAG_CANCEL_CURRENT);
        notificationBuilder.setContentText("It is hazardous to be texting while walking.")
                .setFullScreenIntent(fullScreenPendingIntent, true);
    }//ww w.  j av  a 2 s.  c  om
    Notification notification = notificationBuilder.build();
    //---set the sound and lights---
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_LIGHTS;

    //---gets an instance of the NotificationManager service---
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

    //---build the notification and issues it
    // with notification manager---
    notificationManager.notify(WEARABLE_NOTIFICATION_ID, notification);
}

From source file:at.flack.receiver.EMailReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();//from  w  w  w. j a v a  2s .  c o  m
    try {
        if (bundle.getString("type").equals("mail")) {
            sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
            if (!sharedPrefs.getBoolean("notification_mail", true))
                return;
            notify = sharedPrefs.getBoolean("notifications", true);
            vibrate = sharedPrefs.getBoolean("vibration", true);
            headsup = sharedPrefs.getBoolean("headsup", true);
            led_color = sharedPrefs.getInt("notification_light", -16776961);
            boolean all = sharedPrefs.getBoolean("all_mail", true);

            if (notify == false)
                return;

            NotificationCompat.Builder mBuilder = null;
            boolean use_profile_picture = false;
            Bitmap profile_picture = null;

            String origin_name = bundle.getString("senderName").equals("") ? bundle.getString("senderMail")
                    : bundle.getString("senderName");

            try {
                profile_picture = RoundedImageView.getCroppedBitmap(Bitmap.createScaledBitmap(
                        ProfilePictureCache.getInstance(context).get(origin_name), 200, 200, false), 300);
                use_profile_picture = true;
            } catch (Exception e) {
                use_profile_picture = false;
            }

            Resources res = context.getResources();

            int lastIndex = bundle.getString("subject").lastIndexOf("=");
            if (lastIndex > 0 && Base64.isBase64(bundle.getString("subject").substring(0, lastIndex))) {
                mBuilder = new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.raven_notification_icon).setContentTitle(origin_name)
                        .setColor(0xFFFF5722)
                        .setContentText(res.getString(R.string.sms_receiver_new_encrypted_mail))
                        .setAutoCancel(true);
                if (use_profile_picture && profile_picture != null) {
                    mBuilder = mBuilder.setLargeIcon(profile_picture);
                } else {
                    mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                            context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
                }
            } else {

                if (bundle.getString("subject").charAt(0) == '%' && (bundle.getString("subject").length() == 10
                        || bundle.getString("subject").length() == 9)) {
                    if (lastIndex > 0 && Base64.isBase64(bundle.getString("subject").substring(1, lastIndex))) {
                        mBuilder = new NotificationCompat.Builder(context).setContentTitle(origin_name)
                                .setColor(0xFFFF5722)
                                .setContentText(res.getString(R.string.sms_receiver_handshake_received))
                                .setSmallIcon(R.drawable.notification_icon).setAutoCancel(true);
                        if (use_profile_picture && profile_picture != null) {
                            mBuilder = mBuilder.setLargeIcon(profile_picture);
                        } else {
                            mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                                    context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
                        }
                    } else {
                        return;
                    }
                } else if (bundle.getString("subject").charAt(0) == '%'
                        && bundle.getString("subject").length() >= 120
                        && bundle.getString("subject").length() < 125) {
                    if (lastIndex > 0 && Base64.isBase64(bundle.getString("subject").substring(1, lastIndex))) {
                        mBuilder = new NotificationCompat.Builder(context).setContentTitle(origin_name)
                                .setColor(0xFFFF5722)
                                .setContentText(res.getString(R.string.sms_receiver_handshake_received))
                                .setSmallIcon(R.drawable.notification_icon).setAutoCancel(true);
                        if (use_profile_picture && profile_picture != null) {
                            mBuilder = mBuilder.setLargeIcon(profile_picture);
                        } else {
                            mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                                    context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
                        }
                    } else {
                        return;
                    }
                } else if (all) { // normal message
                    mBuilder = new NotificationCompat.Builder(context)
                            .setSmallIcon(R.drawable.raven_notification_icon).setContentTitle(origin_name)
                            .setColor(0xFFFF5722).setContentText(bundle.getString("subject"))
                            .setAutoCancel(true).setStyle(
                                    new NotificationCompat.BigTextStyle().bigText(bundle.getString("subject")));

                    if (use_profile_picture && profile_picture != null) {
                        mBuilder = mBuilder.setLargeIcon(profile_picture);
                    } else {
                        mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                                context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
                    }
                } else {
                    return;
                }
            }
            // }
            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            mBuilder.setSound(alarmSound);
            mBuilder.setLights(led_color, 750, 4000);
            if (vibrate) {
                mBuilder.setVibrate(new long[] { 0, 100, 200, 300 });
            }

            Intent resultIntent = new Intent(context, MainActivity.class);
            resultIntent.putExtra("MAIL", bundle.getString("senderMail"));

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(1,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
            if (Build.VERSION.SDK_INT >= 16 && headsup)
                mBuilder.setPriority(Notification.PRIORITY_HIGH);
            if (Build.VERSION.SDK_INT >= 21)
                mBuilder.setCategory(Notification.CATEGORY_MESSAGE);

            final NotificationManager mNotificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);

            if (!MainActivity.isOnTop)
                mNotificationManager.notify(9, mBuilder.build());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:at.flack.receiver.SmsReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    notify = sharedPrefs.getBoolean("notifications", true);
    vibrate = sharedPrefs.getBoolean("vibration", true);
    headsup = sharedPrefs.getBoolean("headsup", true);
    led_color = sharedPrefs.getInt("notification_light", -16776961);
    boolean all = sharedPrefs.getBoolean("all_sms", true);

    if (notify == false)
        return;/*from   w  ww  .j a v a  2  s . c  om*/

    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    if (bundle != null) {
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            str += msgs[i].getMessageBody().toString();
            str += "\n";
        }

        NotificationCompat.Builder mBuilder;

        if (main != null) {
            main.addNewMessage(str);
            return;
        }

        boolean use_profile_picture = false;
        Bitmap profile_picture = null;

        String origin_name = null;
        try {
            origin_name = getContactName(context, msgs[0].getDisplayOriginatingAddress());
            if (origin_name == null)
                origin_name = msgs[0].getDisplayOriginatingAddress();
        } catch (Exception e) {
        }
        if (origin_name == null)
            origin_name = "Unknown";
        try {
            profile_picture = RoundedImageView.getCroppedBitmap(Bitmap.createScaledBitmap(
                    fetchThumbnail(context, msgs[0].getDisplayOriginatingAddress()), 200, 200, false), 300);
            use_profile_picture = true;
        } catch (Exception e) {
            use_profile_picture = false;
        }

        Resources res = context.getResources();
        int positionOfBase64End = str.lastIndexOf("=");
        if (positionOfBase64End > 0 && Base64.isBase64(str.substring(0, positionOfBase64End))) {
            mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.raven_notification_icon)
                    .setContentTitle(origin_name).setColor(0xFFB71C1C)
                    .setContentText(res.getString(R.string.sms_receiver_new_encrypted)).setAutoCancel(true);
            if (use_profile_picture && profile_picture != null) {
                mBuilder = mBuilder.setLargeIcon(profile_picture);
            } else {
                mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                        context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
            }

        } else if (str.toString().charAt(0) == '%'
                && (str.toString().length() == 10 || str.toString().length() == 9)) {
            int lastIndex = str.toString().lastIndexOf("=");
            if (lastIndex > 0 && Base64.isBase64(str.toString().substring(1, lastIndex))) {
                mBuilder = new NotificationCompat.Builder(context).setContentTitle(origin_name)
                        .setColor(0xFFB71C1C)
                        .setContentText(res.getString(R.string.sms_receiver_handshake_received))
                        .setSmallIcon(R.drawable.raven_notification_icon).setAutoCancel(true);
                if (use_profile_picture && profile_picture != null) {
                    mBuilder = mBuilder.setLargeIcon(profile_picture);
                } else {
                    mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                            context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
                }
            } else {
                return;
            }
        } else if (str.toString().charAt(0) == '%' && str.toString().length() >= 120
                && str.toString().length() < 125) { // DH Handshake
            int lastIndex = str.toString().lastIndexOf("=");
            if (lastIndex > 0 && Base64.isBase64(str.toString().substring(1, lastIndex))) {
                mBuilder = new NotificationCompat.Builder(context).setContentTitle(origin_name)
                        .setColor(0xFFB71C1C)
                        .setContentText(res.getString(R.string.sms_receiver_handshake_received))
                        .setSmallIcon(R.drawable.raven_notification_icon).setAutoCancel(true);
                if (use_profile_picture && profile_picture != null) {
                    mBuilder = mBuilder.setLargeIcon(profile_picture);
                } else {
                    mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                            context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
                }
            } else {
                return;
            }
        } else { // unencrypted messages
            if (all) {
                mBuilder = new NotificationCompat.Builder(context).setContentTitle(origin_name)
                        .setColor(0xFFB71C1C).setContentText(str).setAutoCancel(true)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(str))
                        .setSmallIcon(R.drawable.raven_notification_icon);
                if (use_profile_picture && profile_picture != null) {
                    mBuilder = mBuilder.setLargeIcon(profile_picture);
                } else {
                    mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                            context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
                }
            } else {
                return;
            }
        }

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(alarmSound);
        mBuilder.setLights(led_color, 750, 4000);
        if (vibrate) {
            mBuilder.setVibrate(new long[] { 0, 100, 200, 300 });
        }

        Intent resultIntent = new Intent(context, MainActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        resultIntent.putExtra("CONTACT_NUMBER", msgs[0].getOriginatingAddress());

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        if (Build.VERSION.SDK_INT >= 16 && headsup)
            mBuilder.setPriority(Notification.PRIORITY_HIGH);
        if (Build.VERSION.SDK_INT >= 21)
            mBuilder.setCategory(Notification.CATEGORY_MESSAGE);

        final NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED") && !MainActivity.isOnTop)
            mNotificationManager.notify(7, mBuilder.build());

        // Save SMS if default app
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT
                && Telephony.Sms.getDefaultSmsPackage(context).equals(context.getPackageName())) {
            ContentValues values = new ContentValues();
            values.put("address", msgs[0].getDisplayOriginatingAddress());
            values.put("body", str.replace("\n", "").replace("\r", ""));

            if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
                context.getContentResolver().insert(Telephony.Sms.Inbox.CONTENT_URI, values);

        }
    }

}

From source file:com.example.android.wearable.wear.wearnotifications.handlers.MessagingIntentService.java

private NotificationCompat.Builder recreateBuilderWithMessagingStyle() {

    // Main steps for building a MESSAGING_STYLE notification (for more detailed comments on
    // building this notification, check StandaloneMainActivity.java)::
    //      0. Get your data
    //      1. Build the MESSAGING_STYLE
    //      2. Add support for Wear 1.+
    //      3. Set up main Intent for notification
    //      4. Set up RemoteInput (users can input directly from notification)
    //      5. Build and issue the notification

    // 0. Get your data (everything unique per Notification)
    MockDatabase.MessagingStyleCommsAppData messagingStyleCommsAppData = MockDatabase.getMessagingStyleData();

    // 1. Build the Notification.Style (MESSAGING_STYLE)
    String contentTitle = messagingStyleCommsAppData.getContentTitle();

    MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(
            messagingStyleCommsAppData.getReplayName()).setConversationTitle(contentTitle);

    // Adds all Messages
    for (MessagingStyle.Message message : messagingStyleCommsAppData.getMessages()) {
        messagingStyle.addMessage(message);
    }//from   w  w w.j a  v a  2s .com

    // 2. Add support for Wear 1.+.
    String fullMessageForWearVersion1 = messagingStyleCommsAppData.getFullConversation();

    Notification chatHistoryForWearV1 = new NotificationCompat.Builder(getApplicationContext())
            .setStyle(new NotificationCompat.BigTextStyle().bigText(fullMessageForWearVersion1))
            .setContentTitle(contentTitle).setSmallIcon(R.drawable.ic_launcher)
            .setContentText(fullMessageForWearVersion1).build();

    // Adds page with all text to support Wear 1.+.
    NotificationCompat.WearableExtender wearableExtenderForWearVersion1 = new NotificationCompat.WearableExtender()
            .setHintContentIntentLaunchesActivity(true).addPage(chatHistoryForWearV1);

    // 3. Set up main Intent for notification
    Intent notifyIntent = new Intent(this, MessagingMainActivity.class);

    PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // 4. Set up a RemoteInput Action, so users can input (keyboard, drawing, voice) directly
    // from the notification without entering the app.
    String replyLabel = getString(R.string.reply_label);
    RemoteInput remoteInput = new RemoteInput.Builder(MessagingIntentService.EXTRA_REPLY).setLabel(replyLabel)
            .build();

    Intent replyIntent = new Intent(this, MessagingIntentService.class);
    replyIntent.setAction(MessagingIntentService.ACTION_REPLY);
    PendingIntent replyActionPendingIntent = PendingIntent.getService(this, 0, replyIntent, 0);

    // Enable action to appear inline on Wear 2.0 (24+). This means it will appear over the
    // lower portion of the Notification for easy action (only possible for one action).
    final NotificationCompat.Action.WearableExtender inlineActionForWear2_0 = new NotificationCompat.Action.WearableExtender()
            .setHintDisplayActionInline(true).setHintLaunchesActivity(false);

    NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
            R.drawable.ic_reply_white_18dp, replyLabel, replyActionPendingIntent).addRemoteInput(remoteInput)
                    // Allows system to generate replies by context of conversation
                    .setAllowGeneratedReplies(true)
                    // Add WearableExtender to enable inline actions
                    .extend(inlineActionForWear2_0).build();

    // 5. Build and issue the notification
    NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(
            getApplicationContext());

    GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);

    // Builds and issues notification
    notificationCompatBuilder.setStyle(messagingStyle).setContentTitle(contentTitle)
            .setContentText(messagingStyleCommsAppData.getContentText()).setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_person_black_48dp))
            .setContentIntent(mainPendingIntent).setColor(getResources().getColor(R.color.colorPrimary))
            .setSubText(Integer.toString(messagingStyleCommsAppData.getNumberOfNewMessages()))
            .addAction(replyAction).setCategory(Notification.CATEGORY_MESSAGE)
            .setPriority(Notification.PRIORITY_HIGH).setVisibility(Notification.VISIBILITY_PRIVATE)
            .extend(wearableExtenderForWearVersion1);

    for (String name : messagingStyleCommsAppData.getParticipants()) {
        notificationCompatBuilder.addPerson(name);
    }

    return notificationCompatBuilder;
}

From source file:at.flack.receiver.FacebookReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();//from w  w  w  .  java 2s  .  c  o  m
    try {
        if (main instanceof FbMessageOverview && bundle.getString("type").equals("message")) {
            ((FbMessageOverview) main).addNewMessage(
                    new FacebookMessage(bundle.getString("fb_name"), bundle.getString("fb_message"),
                            bundle.getString("fb_img"), bundle.getLong("fb_id"), bundle.getString("fb_tid")));
        } else if (main instanceof FbMessageOverview && bundle.getString("type").equals("readreceipt")) {
            ((FbMessageOverview) main).changeReadReceipt(bundle.getLong("fb_time"),
                    bundle.getLong("fb_reader"));
        } else if (main instanceof FbMessageOverview && bundle.getString("type").equals("typ")) {
            ((FbMessageOverview) main).changeTypingStatus(bundle.getLong("my_id"), bundle.getLong("fb_id"),
                    bundle.getBoolean("fb_from_mobile"), bundle.getInt("fb_status"));
        } else if (main instanceof FbMessageOverview && bundle.getString("type").equals("img_message")) {
            ((FbMessageOverview) main).addNewMessage(new FacebookImageMessage(bundle.getString("fb_name"),
                    bundle.getString("fb_image"), bundle.getString("fb_preview"), bundle.getString("fb_img"),
                    bundle.getLong("fb_id"), bundle.getString("fb_tid")));
        } else if (bundle.getString("type").equals("read")) {
            killNotification(context,
                    new MarkAsRead(bundle.getString("fb_tid"), bundle.getBoolean("fb_markas")));
        } else if (bundle.getString("type").equals("message")
                || bundle.getString("type").equals("img_message")) {
            sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
            if (!sharedPrefs.getBoolean("notification_fbs", true))
                return;
            notify = sharedPrefs.getBoolean("notifications", true);
            vibrate = sharedPrefs.getBoolean("vibration", true);
            headsup = sharedPrefs.getBoolean("headsup", true);
            led_color = sharedPrefs.getInt("notification_light", -16776961);
            boolean all = sharedPrefs.getBoolean("all_fb", true);

            if (notify == false)
                return;
            if (bundle.getLong("fb_id") == bundle.getLong("my_id")) {
                return;
            }

            NotificationCompat.Builder mBuilder = null;
            boolean use_profile_picture = false;
            Bitmap profile_picture = null;

            String origin_name = bundle.getString("fb_name");

            try {
                profile_picture = RoundedImageView.getCroppedBitmap(Bitmap.createScaledBitmap(
                        ProfilePictureCache.getInstance(context).get(origin_name), 200, 200, false), 300);
                use_profile_picture = true;
            } catch (Exception e) {
                use_profile_picture = false;
            }

            Resources res = context.getResources();

            if (bundle.getString("type").equals("img_message")) {
                bundle.putString("fb_message", res.getString(R.string.fb_notification_new_image));
            }

            int lastIndex = bundle.getString("fb_message").lastIndexOf("=");
            if (lastIndex > 0 && Base64.isBase64(bundle.getString("fb_message").substring(0, lastIndex))) {
                mBuilder = new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.raven_notification_icon).setContentTitle(origin_name)
                        .setColor(0xFF175ea2)
                        .setContentText(res.getString(R.string.sms_receiver_new_encrypted_fb))
                        .setAutoCancel(true);
                if (use_profile_picture && profile_picture != null) {
                    mBuilder = mBuilder.setLargeIcon(profile_picture);
                } else {
                    mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                            context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
                }
            } else { // normal or handshake

                if (bundle.getString("fb_message").charAt(0) == '%'
                        && (bundle.getString("fb_message").length() == 10
                                || bundle.getString("fb_message").length() == 9)) {
                    if (lastIndex > 0
                            && Base64.isBase64(bundle.getString("fb_message").substring(1, lastIndex))) {
                        mBuilder = new NotificationCompat.Builder(context).setContentTitle(origin_name)
                                .setColor(0xFF175ea2)
                                .setContentText(res.getString(R.string.sms_receiver_handshake_received))
                                .setSmallIcon(R.drawable.notification_icon).setAutoCancel(true);
                        if (use_profile_picture && profile_picture != null) {
                            mBuilder = mBuilder.setLargeIcon(profile_picture);
                        } else {
                            mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                                    context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
                        }
                    } else {
                        return;
                    }
                } else if (bundle.getString("fb_message").charAt(0) == '%'
                        && bundle.getString("fb_message").length() >= 120
                        && bundle.getString("fb_message").length() < 125) {
                    if (lastIndex > 0
                            && Base64.isBase64(bundle.getString("fb_message").substring(1, lastIndex))) {
                        mBuilder = new NotificationCompat.Builder(context).setContentTitle(origin_name)
                                .setColor(0xFF175ea2)
                                .setContentText(res.getString(R.string.sms_receiver_handshake_received))
                                .setSmallIcon(R.drawable.notification_icon).setAutoCancel(true);
                        if (use_profile_picture && profile_picture != null) {
                            mBuilder = mBuilder.setLargeIcon(profile_picture);
                        } else {
                            mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                                    context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
                        }
                    } else {
                        return;
                    }
                } else if (all) { // normal message
                    mBuilder = new NotificationCompat.Builder(context)
                            .setSmallIcon(R.drawable.raven_notification_icon).setContentTitle(origin_name)
                            .setColor(0xFF175ea2).setContentText(bundle.getString("fb_message"))
                            .setAutoCancel(true).setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(bundle.getString("fb_message")));

                    if (use_profile_picture && profile_picture != null) {
                        mBuilder = mBuilder.setLargeIcon(profile_picture);
                    } else {
                        mBuilder = mBuilder.setLargeIcon(convertToBitmap(origin_name,
                                context.getResources().getDrawable(R.drawable.ic_social_person), 200, 200));
                    }
                } else {
                    return;
                }
            }
            // }
            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            mBuilder.setSound(alarmSound);
            mBuilder.setLights(led_color, 750, 4000);
            if (vibrate) {
                mBuilder.setVibrate(new long[] { 0, 100, 200, 300 });
            }

            Intent resultIntent = new Intent(context, MainActivity.class);
            resultIntent.putExtra("FACEBOOK_NAME", origin_name);

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(1,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
            if (Build.VERSION.SDK_INT >= 16 && headsup)
                mBuilder.setPriority(Notification.PRIORITY_HIGH);
            if (Build.VERSION.SDK_INT >= 21)
                mBuilder.setCategory(Notification.CATEGORY_MESSAGE);

            final NotificationManager mNotificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);

            if (!MainActivity.isOnTop)
                mNotificationManager.notify(8, mBuilder.build());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}