From 84e4167dbdaeb7a419693f1ba0978601b247a0bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Mon, 18 Oct 2021 00:11:46 +0200 Subject: [PATCH] Mi Band notification improvements (#1579) --- .../notifications/AppNotificationManager.kt | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/notifications/AppNotificationManager.kt b/app/src/main/java/io/github/wulkanowy/services/sync/notifications/AppNotificationManager.kt index 69d0092c1..ddad9bf27 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/notifications/AppNotificationManager.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/notifications/AppNotificationManager.kt @@ -4,6 +4,7 @@ import android.annotation.SuppressLint import android.app.PendingIntent import android.content.Context import android.os.Build +import androidx.annotation.PluralsRes import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import dagger.hilt.android.qualifiers.ApplicationContext @@ -68,7 +69,8 @@ class AppNotificationManager @Inject constructor( ) { val groupType = notificationData.type.group ?: return val group = "${groupType}_${student.id}" - val groupId = student.id * 100 + notificationData.type.ordinal + + notificationData.sendSummaryNotification(group, student) notificationData.lines.forEach { item -> val title = context.resources.getQuantityString(notificationData.titleStringRes, 1) @@ -88,16 +90,26 @@ class AppNotificationManager @Inject constructor( saveNotification(title, item, notificationData, student) } + } + private fun MultipleNotificationsData.sendSummaryNotification(group: String, student: Student) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return - val summaryNotification = getDefaultNotificationBuilder(notificationData) - .setSmallIcon(notificationData.icon) + val summaryNotification = getDefaultNotificationBuilder(this) + .setSmallIcon(icon) + .setContentTitle(getQuantityString(titleStringRes, lines.size)) + .setContentText(getQuantityString(contentStringRes, lines.size)) + .setStyle( + NotificationCompat.InboxStyle() + .setSummaryText(student.nickOrName) + .also { builder -> lines.forEach { builder.addLine(it) } } + ) + .setLocalOnly(true) .setGroup(group) - .setStyle(NotificationCompat.InboxStyle().setSummaryText(student.nickOrName)) .setGroupSummary(true) .build() + val groupId = student.id * 100 + type.ordinal notificationManager.notify(groupId.toInt(), summaryNotification) } @@ -116,6 +128,7 @@ class AppNotificationManager @Inject constructor( .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(NotificationCompat.PRIORITY_HIGH) .setColor(context.getCompatColor(R.color.colorPrimary)) + .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY) .setContentIntent( PendingIntent.getActivity( context, @@ -142,4 +155,8 @@ class AppNotificationManager @Inject constructor( notificationRepository.saveNotification(notificationEntity) } -} \ No newline at end of file + + private fun getQuantityString(@PluralsRes res: Int, arg: Int): String { + return context.resources.getQuantityString(res, arg, arg) + } +}