Lesson 186. Notifications. advanced messages

Lesson 186. Notifications. advanced messages


Android 4.1 (API 16) has advanced messages. Pulling down the message will display additional information.

To create an advanced message, you must add a style to the builder. There are several styles. They are all heirs to the abstract class NotificationCompat.Style. It is usually clear from the style name what it can be used for.

Consider, for example, the BigTextStyle style to reflect long text.

Next I will give only the code of the builder. And how to get and display messages from the builder, you can see in the previous lessons.

code:

String longText = "To have a notification appear in an expanded view, " +
       "first create a NotificationCompat.Builder object " +
       "with the normal view options you want. " +
       "Next, call Builder.setStyle() with an " +
       "expanded layout object as its argument.";

NotificationCompat.Builder builder =
       new NotificationCompat.Builder(this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setContentTitle("Title")
               .setContentText("Notification text")
               .setStyle(new NotificationCompat.BigTextStyle().bigText(longText));

We call the setStyle method in the message builder, in which we need to pass the style. We create a BigTextStyle style and pass it a long text to the bigText method.

The message will now display a long text message.

The BigPictureStyle style will help to show the big picture:

BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.startandroid, options);

NotificationCompat.Builder builder =
       new NotificationCompat.Builder(this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setContentTitle("Title")
               .setContentText("Notification text")
               .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap));

We create a bitmap and pass it to BigPictureStyle.

When opened, the message will display a picture

InboxStyle Style – Lists up to 5 of your rows

NotificationCompat.Builder builder =
       new NotificationCompat.Builder(this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setContentTitle("Title")
               .setContentText("Notification text")
               .setStyle(new NotificationCompat.InboxStyle()
                       .addLine("Line 1")
                       .addLine("Line 2")
                       .addLine("Line 3"));

The addLine method adds rows

result:

MessagingStyle style is convenient for displaying recent chat messages:

NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle("You");
messagingStyle.setConversationTitle("Android chat")
       .addMessage("Всем привет!", System.currentTimeMillis(), "Ivan")
       .addMessage("Кто перешел на новую студию, как оно?", System.currentTimeMillis(), "Ivan")
       .addMessage("Я пока не переходил, жду отзывов", System.currentTimeMillis(), "Andrey")
       .addMessage("Я перешел", System.currentTimeMillis(), null)
       .addMessage("Было несколько проблем, но все решаемо", System.currentTimeMillis(), null)
       .addMessage("Ок, спасибо!", System.currentTimeMillis(), "Ivan");

NotificationCompat.Builder builder =
       new NotificationCompat.Builder(this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setContentTitle("Title")
               .setContentText("Notification text")
               .setStyle(messagingStyle);

In the MessagingStyle constructor, you need to specify under which name the user will see your messages in this. Usually, in chats, you use the word You (or Me).

IN setConversationTitle the name of the chat is specified. This is usually used when chatting with more than two people.

Next, the method addMessage messages are added. The message consists of three fields: text, time, sender. If the sender is null, then this is considered a user message, and the name we specified in the MessagingStyle constructor, ie You, will be used.

You can add as many messages as you want, but once the number of messages is greater than MessagingStyle.MAXIMUM_RETAINED_MESSAGES, old messages will start to be deleted. It is convenient and allows not to bother checking for quantity.

Messages will be displayed in the same order that you added them.

It will look like this:

Message time is not displayed. As far as I understand, superficially looking the weekend, it is not used anywhere at all. Maybe in future releases this will change.

The addMessage method also works with the Message object. This object contains fields: text, time and author. But there is a setData method for specifying MIME data, such as pictures.

Some styles have a couple of common methods that may be useful: setBigContentTitle and setSummaryText.

Consider them in the InboxStyle example:

NotificationCompat.Builder builder =
       new NotificationCompat.Builder(this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setContentTitle("Title")
               .setContentText("Notification text")
               .setStyle(new NotificationCompat.InboxStyle()
                       .addLine("Line 1")
                       .addLine("Line 2")
                       .addLine("Line 3")
                       .setBigContentTitle("Extended title")
                       .setSummaryText("+5 more"));

The code is almost the same as what we looked at earlier. Two methods are added:

setBigContentTitle – allows you to specify the message header that will be displayed when the message is opened

setSummaryText – The text will be displayed at the bottom of the extended message

result:

When the message is opened, the title changes and the summary text is displayed at the bottom.




Discuss in the forum [1 reply]

Leave a Comment