Fixed: Application crashes on FetchDownloadNotificationManager.getCancelNotification method.

* The issue occurred when the user paused a download from the notification while the application was in the background and only a single download was active. At that point, a paused notification was posted on the IO thread to inform the user, and shortly after, the same notification was set on the foreground service. Due to a timing conflict, both notifications were being processed simultaneously, leading to a crash.
* To fix this, a synchronization lock is added inside the `getCancelNotification` method to ensure that only one thread can access the notification builder at a time.
This commit is contained in:
MohitMaliFtechiz 2025-04-09 11:29:46 +05:30
parent ef9a7a2129
commit 56b94cb5cf

View File

@ -73,6 +73,8 @@ class FetchDownloadNotificationManager @Inject constructor(
val context: Context, val context: Context,
private val downloadRoomDao: DownloadRoomDao private val downloadRoomDao: DownloadRoomDao
) : DefaultFetchNotificationManager(context) { ) : DefaultFetchNotificationManager(context) {
private val notificationBuilderLock = Any()
private val downloadNotificationManager: NotificationManager by lazy { private val downloadNotificationManager: NotificationManager by lazy {
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
} }
@ -258,6 +260,7 @@ class FetchDownloadNotificationManager @Inject constructor(
download: Download, download: Download,
notificationBuilder: Builder notificationBuilder: Builder
): Notification { ): Notification {
synchronized(notificationBuilderLock) {
val downloadTitle = getDownloadNotificationTitle(download) val downloadTitle = getDownloadNotificationTitle(download)
val notificationTitle = val notificationTitle =
runBlocking(Dispatchers.IO) { runBlocking(Dispatchers.IO) {
@ -287,6 +290,7 @@ class FetchDownloadNotificationManager @Inject constructor(
) )
.build() .build()
} }
}
private fun getActionPendingIntent( private fun getActionPendingIntent(
fetch: Fetch, fetch: Fetch,