create notification group using summary notification

Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
parneet-guraya 2024-01-10 01:35:27 +05:30 committed by Marcel Hibbe
parent d118bf06a4
commit 94a1238f28
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
2 changed files with 36 additions and 1 deletions

View File

@ -168,7 +168,7 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
}
if (uploadSuccess) {
mNotifyManager?.cancel(notificationId)
cancelNotification()
return Result.success()
} else if (isStopped) {
// since work is cancelled the result would be ignored anyways
@ -230,6 +230,7 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
.setOngoing(true)
.setProgress(HUNDRED_PERCENT, ZERO_PERCENT, false)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setGroup(NotificationUtils.KEY_UPLOAD_GROUP)
.setContentIntent(getIntentToOpenConversation())
.addAction(
R.drawable.ic_cancel_white_24dp, getResourceString(context, R.string.nc_cancel),
@ -237,8 +238,39 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
)
.build()
notificationId = SystemClock.uptimeMillis().toInt()
mNotifyManager!!.notify(notificationId, notification)
// only need one summary notification but multiple upload worker can call it more than once but it is safe
// because of the same notification object config and id.
makeSummaryNotification()
}
private fun makeSummaryNotification() {
// summary notification encapsulating the group of notifications
val summaryNotification = NotificationCompat.Builder(
context, NotificationUtils.NotificationChannels
.NOTIFICATION_CHANNEL_UPLOADS.name
).setSmallIcon(R.drawable.upload_white)
.setGroup(NotificationUtils.KEY_UPLOAD_GROUP)
.setGroupSummary(true)
.build()
mNotifyManager?.notify(NotificationUtils.GROUP_SUMMARY_NOTIFICATION_ID, summaryNotification)
}
private fun getActiveUploadNotifications(): Int? {
// filter out active notifications that are upload notifications using group
return mNotifyManager?.activeNotifications?.filter { notification.group == NotificationUtils.KEY_UPLOAD_GROUP }?.size
}
private fun cancelNotification() {
mNotifyManager?.cancel(notificationId)
// summary notification would not get dismissed automatically if child notifications are cancelled programmatically
// so check if only 1 notification left if yes then cancel it because that would be summary notification
if(getActiveUploadNotifications() == 1){
mNotifyManager?.cancel(NotificationUtils.GROUP_SUMMARY_NOTIFICATION_ID)
}
}
private fun getNotificationContentText(percentage: Int): String {

View File

@ -68,6 +68,9 @@ object NotificationUtils {
// RemoteInput key - used for replies sent directly from notification
const val KEY_DIRECT_REPLY = "key_direct_reply"
// notification group keys
const val KEY_UPLOAD_GROUP = "com.nextcloud.talk.utils.KEY_UPLOAD_GROUP"
const val GROUP_SUMMARY_NOTIFICATION_ID = -1
@TargetApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(
context: Context,