Lesson 189. Notifications. message grouping

Lesson 189. Notifications. message grouping


Android 7 (API 24) has the ability to group messages. Even if you don’t explicitly implement it, the system will create the group itself if you have 4 or more messages.

It looks like this:

You can expand / collapse / delete a group. You can also tap or delete a separate message. If the number of messages in a group is less than 4, then the messages become separate from the group.

All your messages will fall into one unnamed group. By default, clicking on this group will open the application.

Let’s look at what group management options we have been given.

We can:

1) create multiple groups and decide for yourself which one the new message will go to. A group will be displayed when there are at least two messages in it.

2) add text description to the group

3) Add to the group PendingIntent, which will work after clicking on the group

An example is the mail application. The user has several accounts and new emails are coming in. It would be wise to create a separate group for each account. Each such group will only display emails that are in a specific account.

Let’s consider in practice. Create a message:

NotificationCompat.Builder mBuilder =
       new NotificationCompat.Builder(this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setContentTitle("Sender " + 1)
               .setContentText("Subject text " + 1)
               .setGroup(GROUP_KEY);

Notification notification = mBuilder.build();

NotificationManager notificationManager =
       (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);

The message is created as usual. Only setGroup method added. In it, we need to specify a String group key to let the system know which group to post validity messages to.

Create a group:

NotificationCompat.Builder mBuilder =
       new NotificationCompat.Builder(this)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setContentInfo("user_mail.com")
               .setGroup(GROUP_KEY)
               .setGroupSummary(true);

Notification notification = mBuilder.build();

NotificationManager notificationManager =
       (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(-100, notification);

The group is created just like the message. But the texts we usually pass to setContentTitle and setContentTitle are not displayed in the group, so I don’t use them in the example. However, in later versions of Android, the display of groups may change, and they will suddenly start to display titile and text. It can and does make sense to transfer anything there.

But the text from the setContentInfo method will be displayed and this allows us to provide some text description for the group so that the user understands what it is about. Since we are considering an example mail program, you can use the username of the user account. The user will see in which group the content of which account is displayed.

In setGroup we pass the String group key. We use it to create messages that should fall into this group.

In setGroupSummary we specify true. This means that the message is a group.

In the notify method, just like with a regular message, you must specify an id. Be careful here to make sure that the messages and groups do not match the id. The band is still a regular message. And if the id matches, one message will replace the other.

Alternatively, emails for new emails can be retrieved and group ids for account id. Then they don’t exactly intersect.

As a result, you get the following picture

Three new letters came together in one group. And the description of the group shows that it reflects the letters of the account This e-mail address is being protected from spambots. You need JavaScript enabled to view it..

When to create a group

GroupsSense to display every time you create a message that belongs to this group. If a group already exists and is displayed, then nothing will happen (unless you have changed the group settings).

And if we only display the group once and the user at some point removes it from the messages, then all our new messages will be left out of the group.

Sort in group

You can sort messages in a group using the setSortKey method in the builder of each message. Pass a string value to it, and sort it.




Discuss in the forum [1 reply]
Related Posts
Lesson 190. Notifications. channels

Android Oreo (API 26) has the ability to create message channels. In this lesson we will understand how to Read more

Lesson 188. Notifications. custom messages

Android enables us to create a layout for messages ourselves. Consider a simple example: layout / notification.xml Height 64dp Read more

Lesson 187. Notifications. Action buttons. Reply.

Android 4.1 has the ability to add buttons to messages. This is done using the addAction method. Intent deleteIntent Read more

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 Read more

Leave a Comment