Merge pull request #4240 from kiwix/Fixes#4237

Fixed: Duplicate download completed notifications showing.
This commit is contained in:
Kelson 2025-02-27 17:36:33 +01:00 committed by GitHub
commit ed5f7160f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 2 deletions

View File

@ -48,6 +48,8 @@ import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.utils.DOWNLOAD_NOTIFICATION_CHANNEL_ID
import javax.inject.Inject
const val THIRTY_TREE = 33
class DownloadMonitorService : Service() {
private val updater = PublishSubject.create<() -> Unit>()
private var updaterDisposable: Disposable? = null
@ -265,7 +267,6 @@ class DownloadMonitorService : Service() {
}
}
@Suppress("MagicNumber")
private fun showDownloadCompletedNotification(download: Download) {
downloadNotificationChannel()
val notificationBuilder = getNotificationBuilder(download.id)
@ -287,7 +288,10 @@ class DownloadMonitorService : Service() {
// notification. If we use the same ID, changing the foreground notification for another
// ongoing download cancels the previous notification for that id, preventing the download
// complete notification from being displayed.
val downloadCompleteNotificationId = download.id + 33
val downloadCompleteNotificationId = download.id + THIRTY_TREE
// Cancel the complete download notification if already shown due to the application's
// lifecycle fetch. See #4237 for more details.
cancelNotificationForId(download.id - THIRTY_TREE)
notificationManager.notify(downloadCompleteNotificationId, notificationBuilder.build())
}

View File

@ -181,6 +181,28 @@ class FetchDownloadNotificationManager @Inject constructor(
else -> notificationBuilder.setTimeoutAfter(DEFAULT_NOTIFICATION_TIMEOUT_AFTER_RESET)
}
notificationCustomisation(downloadNotification, notificationBuilder, context)
// Remove the already shown notification if any, because fetch now pushes a
// download complete notification.
removeNotificationIfAlreadyShowingForCompletedDownload(downloadNotification)
}
/**
* We are adding 33 to the groupId (which is the download ID) because the download
* complete notification is shown by DownloadMonitorService. If the application resumes
* just before the download completes, Fetch in the application might also push a
* download complete notification.
*
* To avoid duplicate notifications, we clear the previous notification if it is already shown.
* See #4237 for more information.
*
* @see DownloadMonitorService.showDownloadCompletedNotification
*/
private fun removeNotificationIfAlreadyShowingForCompletedDownload(
downloadNotification: DownloadNotification
) {
if (downloadNotification.isCompleted) {
downloadNotificationManager.cancel(downloadNotification.groupId + THIRTY_TREE)
}
}
@SuppressLint("UnspecifiedImmutableFlag")