⬆️ ⬇️

Grouping alerts in Android 7

After updating to Android 7, we noticed that notifications began to be grouped by application automatically. Since at the time of the release of the new version of Android, our application did not yet have a notification history, this change was quite critical - only 7 notifications are displayed in one group. This means that if more than 20 notifications arrive per day through the application, you need to brush off new ones in order to see older ones.



Notifications in the group are sorted by priority. If you have 7 notifications in the group and another low priority comes, it will first be at the top, and then fly to the bottom and you will not see it at all in the list until you delete a certain number of notifications.



We made grouping by channels, this allows you to immediately see the channel and, if possible, minimize it. Even if the channel sends you 100 notifications, they are still easily collapsed and removed in one motion.

')

In this article we will explain why the official manual does not work, and how we found a way to do it right.



What does the official manual say?







If you go here , then we are simply recommended "You can use the Builder.setGroup () method to bundle similar notifications". And further recommend to go here .





It only works on Android Wear. We found a solution by typing, however, while writing the article, another manual was found in the article . It is partially correct, but very little is described.



How we did



The manual above says: “It is important to also create a summary notification. This is a notification notice that you’ve signed out on setGroupSummary (true). It is an opportune time to use the InboxStyle, although it is not a requirement. There is no need for any specific information. . "



Here those sores immediately pop up to which we ran.



1. You need to limit the version of the API



Before Android 7, it is better to turn off grouping altogether - otherwise you will not see notifications in the group when using our solution.



Boolean groupflag = false; if(Build.VERSION.SDK_INT > 23){ groupflag = true; } 


2. Auto-close and re-create



If setAutoCancel (true) is not specified - when you click on the last notification from the group, the group will remain on the list. When another notification comes from a group, you can re-create the main header with the same ID - for identification we use the channel ID (subid) for such notification, and the group header is set via setContentTitle.



 if(groupflag) { Notification mainnotif = new NotificationCompat.Builder(this) .setContentTitle(subname) .setAutoCancel(true) .setSmallIcon(R.mipmap.ic_launcher) .setGroupSummary(true) .setGroup(subname) .setStyle(new NotificationCompat.MessagingStyle(subname)) .setColor(ContextCompat.getColor(context, R.color.blue)) .setSubText(subname) .build(); notificationManager.notify(subid, mainnotif); } 


3. Creating the notification itself



Do not forget setGroup (subname). Also an interesting life hack, if you just need to turn off the standard grouping - you can add, for example, setGroup (“App”) without creating a header notification. Then the grouping on the device itself will not work, but to disable groups also on the clock, I think it’s worth writing there random text. Without a group, notifications will be grouped by your application.



  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( context).setSmallIcon(R.mipmap.ic_launcher) .setAutoCancel(true) .setWhen(time) .setTicker(title) .setLargeIcon(icon) .setContentTitle(title) .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg) .setVibrate(new long[0]) .setSubText(subname) .setGroupSummary(false) .setColor(ContextCompat.getColor(context, R.color.blue)); if(groupflag) mBuilder.setGroup(subname); 


You can try such notifications in our application , just add a device to your account and ping through your personal account in your profile 4-6 times. This implementation allows you to save space in the notification area and efficiently manage them. I very often see how applications either replace previous notifications, essentially removing the opportunity to read it, or simply do not use grouping where it is possible.



Also, a little detail, if you believe the metadata section “the addPerson () method allows you to add a list of people to the notification. With its help, your notification may signal the system that it should group notifications from the specified people or consider notifications from these people more important. However, we have not tried this method.

Source: https://habr.com/ru/post/319794/



All Articles