Clean up stuck notifications when service gets killed
This commit is contained in:
parent
c7880b8a8b
commit
b0c6eeb9f7
2 changed files with 16 additions and 2 deletions
|
@ -53,7 +53,7 @@ class ConfigurableBackupTransportService : Service(), KoinComponent {
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
notificationManager.onBackupBackgroundFinished()
|
notificationManager.onServiceDestroyed()
|
||||||
transport = null
|
transport = null
|
||||||
Log.d(TAG, "Service destroyed.")
|
Log.d(TAG, "Service destroyed.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -144,8 +144,22 @@ internal class BackupNotificationManager(private val context: Context) {
|
||||||
nm.notify(NOTIFICATION_ID_BACKGROUND, notification)
|
nm.notify(NOTIFICATION_ID_BACKGROUND, notification)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onBackupBackgroundFinished() {
|
fun onServiceDestroyed() {
|
||||||
nm.cancel(NOTIFICATION_ID_BACKGROUND)
|
nm.cancel(NOTIFICATION_ID_BACKGROUND)
|
||||||
|
// Cancel left-over notifications that are still ongoing.
|
||||||
|
//
|
||||||
|
// We have seen a race condition where the service was taken down at the same time
|
||||||
|
// as BackupObserver#backupFinished() was called, early enough to miss the cancel.
|
||||||
|
//
|
||||||
|
// This won't bring back the expected finish notification in this case,
|
||||||
|
// but at least we don't leave stuck notifications laying around.
|
||||||
|
nm.activeNotifications.forEach { notification ->
|
||||||
|
// only consider ongoing notifications in our ID space (storage backup uses > 1000)
|
||||||
|
if (notification.isOngoing && notification.id < 1000) {
|
||||||
|
Log.w(TAG, "Needed to clean up notification with ID ${notification.id}")
|
||||||
|
nm.cancel(notification.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onBackupFinished(success: Boolean, numBackedUp: Int?) {
|
fun onBackupFinished(success: Boolean, numBackedUp: Int?) {
|
||||||
|
|
Loading…
Reference in a new issue