Browse Source

create notification group using summary notification

Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
parneet-guraya 1 year ago
parent
commit
94a1238f28

+ 33 - 1
app/src/main/java/com/nextcloud/talk/jobs/UploadAndShareFilesWorker.kt

@@ -168,7 +168,7 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
             }
             }
 
 
             if (uploadSuccess) {
             if (uploadSuccess) {
-                mNotifyManager?.cancel(notificationId)
+                cancelNotification()
                 return Result.success()
                 return Result.success()
             } else if (isStopped) {
             } else if (isStopped) {
                 // since work is cancelled the result would be ignored anyways
                 // since work is cancelled the result would be ignored anyways
@@ -230,6 +230,7 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
             .setOngoing(true)
             .setOngoing(true)
             .setProgress(HUNDRED_PERCENT, ZERO_PERCENT, false)
             .setProgress(HUNDRED_PERCENT, ZERO_PERCENT, false)
             .setPriority(NotificationCompat.PRIORITY_LOW)
             .setPriority(NotificationCompat.PRIORITY_LOW)
+            .setGroup(NotificationUtils.KEY_UPLOAD_GROUP)
             .setContentIntent(getIntentToOpenConversation())
             .setContentIntent(getIntentToOpenConversation())
             .addAction(
             .addAction(
                 R.drawable.ic_cancel_white_24dp, getResourceString(context, R.string.nc_cancel),
                 R.drawable.ic_cancel_white_24dp, getResourceString(context, R.string.nc_cancel),
@@ -237,8 +238,39 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
             )
             )
             .build()
             .build()
 
 
+
         notificationId = SystemClock.uptimeMillis().toInt()
         notificationId = SystemClock.uptimeMillis().toInt()
         mNotifyManager!!.notify(notificationId, notification)
         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 {
     private fun getNotificationContentText(percentage: Int): String {

+ 3 - 0
app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.kt

@@ -68,6 +68,9 @@ object NotificationUtils {
     // RemoteInput key - used for replies sent directly from notification
     // RemoteInput key - used for replies sent directly from notification
     const val KEY_DIRECT_REPLY = "key_direct_reply"
     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)
     @TargetApi(Build.VERSION_CODES.O)
     private fun createNotificationChannel(
     private fun createNotificationChannel(
         context: Context,
         context: Context,