Repository: thundernest/k-9
Issue: Notifications on new emails reappear after removal (notification bar)

### Checklist

- [X] I have used the search function to see if someone else has already submitted the same bug report.
- [X] I will describe the problem with as much detail as possible.

### App version

6.202

### Where did you get the app from?

Google Play

### Android version

10

### Device model

Samsung S9

### Steps to reproduce

Notification on new email(s) appears in the notification bar. 
I open the notification bar and see details on all new emails reported by K9. 
When I delete the notification (either by swiping away or by pressing the x) it returns as new notification after 1 or 2 seconds. 
When I delete the notification a second time it stays gone for good. Although, in some (?) cases the notification will pop up again together with another notification on newer emails at a later time point. 

### Expected behavior

Dear Sir or Madam, 
I am writing at your advice from the K9-Forum. Link to the thread: 
https://forum.k9mail.app/t/notifications-on-new-emails-reappear-after-removal/5451
In short: Every email notification appearing in the status bar reappears a second time after I have removed it (mostly swiped away but not only). After its reappearance I usually remove them by swiping them away a second time. And after that they stay gone. 
This applies to all my 6 different email accounts I use in K9. Some of them sync via POP3 and some via IMAP. 
Expected behavior should be: deleted notifications should stay deleted after their first removal.  
Can you help me? I am adding the log file. 
Thank you!
Yukusoona

### Actual behavior

In short: Every email notification appearing in the status bar reappears a second time after I have removed it (mostly swiped away but not only). After its reappearance I usually remove them by swiping them away a second time. And after that they stay gone. 
This applies to all my 6 different email accounts I use in K9. Some of them sync via POP3 and some via IMAP. 


### Logs

[221001-k9mail-logs.txt](https://github.com/thundernest/k-9/files/9852075/221001-k9mail-logs.txt)


Changed files:
app/core/src/main/java/com/fsck/k9/notification/NotificationRepository.kt, app/core/src/main/java/com/fsck/k9/notification/NotificationDataStore.kt, app/core/src/main/java/com/fsck/k9/notification/NewMailNotificationController.kt

diff --git a/app/core/src/main/java/com/fsck/k9/notification/NewMailNotificationController.kt b/app/core/src/main/java/com/fsck/k9/notification/NewMailNotificationController.kt
index c29c7791f..21c850da5 100644
--- a/app/core/src/main/java/com/fsck/k9/notification/NewMailNotificationController.kt
+++ b/app/core/src/main/java/com/fsck/k9/notification/NewMailNotificationController.kt
@@ -14,6 +14,7 @@ internal class NewMailNotificationController(
     private val summaryNotificationCreator: SummaryNotificationCreator,
     private val singleMessageNotificationCreator: SingleMessageNotificationCreator
 ) {
+    @Synchronized
     fun restoreNewMailNotifications(accounts: List<Account>) {
         for (account in accounts) {
             val notificationData = newMailNotificationManager.restoreNewMailNotifications(account)
@@ -24,6 +25,7 @@ internal class NewMailNotificationController(
         }
     }
 
+    @Synchronized
     fun addNewMailNotification(account: Account, message: LocalMessage, silent: Boolean) {
         val notificationData = newMailNotificationManager.addNewMailNotification(account, message, silent)
 
@@ -32,6 +34,7 @@ internal class NewMailNotificationController(
         }
     }
 
+    @Synchronized
     fun removeNewMailNotifications(
         account: Account,
         clearNewMessageState: Boolean,
@@ -48,6 +51,7 @@ internal class NewMailNotificationController(
         }
     }
 
+    @Synchronized
     fun clearNewMailNotifications(account: Account, clearNewMessageState: Boolean) {
         val cancelNotificationIds = newMailNotificationManager.clearNewMailNotifications(account, clearNewMessageState)
 
diff --git a/app/core/src/main/java/com/fsck/k9/notification/NotificationDataStore.kt b/app/core/src/main/java/com/fsck/k9/notification/NotificationDataStore.kt
index de9f8c94b..058f229e8 100644
--- a/app/core/src/main/java/com/fsck/k9/notification/NotificationDataStore.kt
+++ b/app/core/src/main/java/com/fsck/k9/notification/NotificationDataStore.kt
@@ -15,6 +15,11 @@ internal const val MAX_NUMBER_OF_NEW_MESSAGE_NOTIFICATIONS = 8
 internal class NotificationDataStore {
     private val notificationDataMap = mutableMapOf<String, NotificationData>()
 
+    @Synchronized
+    fun isAccountInitialized(account: Account): Boolean {
+        return notificationDataMap[account.uuid] != null
+    }
+
     @Synchronized
     fun initializeAccount(
         account: Account,
diff --git a/app/core/src/main/java/com/fsck/k9/notification/NotificationRepository.kt b/app/core/src/main/java/com/fsck/k9/notification/NotificationRepository.kt
index ddad7bb96..05d26a6a3 100644
--- a/app/core/src/main/java/com/fsck/k9/notification/NotificationRepository.kt
+++ b/app/core/src/main/java/com/fsck/k9/notification/NotificationRepository.kt
@@ -15,14 +15,14 @@ internal class NotificationRepository(
 
     @Synchronized
     fun restoreNotifications(account: Account): NotificationData? {
+        if (notificationDataStore.isAccountInitialized(account)) return null
+
         val localStore = localStoreProvider.getInstance(account)
 
         val (activeNotificationMessages, inactiveNotificationMessages) = localStore.notificationMessages.partition {
             it.notificationId != null
         }
 
-        if (activeNotificationMessages.isEmpty()) return null
-
         val activeNotifications = activeNotificationMessages.map { notificationMessage ->
             val content = notificationContentCreator.createFromMessage(account, notificationMessage.message)
             NotificationHolder(notificationMessage.notificationId!!, notificationMessage.timestamp, content)
@@ -33,11 +33,19 @@ internal class NotificationRepository(
             InactiveNotificationHolder(notificationMessage.timestamp, content)
         }
 
-        return notificationDataStore.initializeAccount(account, activeNotifications, inactiveNotifications)
+        val notificationData = notificationDataStore.initializeAccount(
+            account,
+            activeNotifications,
+            inactiveNotifications
+        )
+
+        return if (notificationData.activeNotifications.isNotEmpty()) notificationData else null
     }
 
     @Synchronized
     fun addNotification(account: Account, content: NotificationContent, timestamp: Long): AddNotificationResult? {
+        restoreNotifications(account)
+
         return notificationDataStore.addNotification(account, content, timestamp)?.also { result ->
             persistNotificationDataStoreChanges(
                 account = account,
@@ -53,6 +61,8 @@ internal class NotificationRepository(
         clearNewMessageState: Boolean = true,
         selector: (List<MessageReference>) -> List<MessageReference>
     ): RemoveNotificationsResult? {
+        restoreNotifications(account)
+
         return notificationDataStore.removeNotifications(account, selector)?.also { result ->
             persistNotificationDataStoreChanges(
                 account = account,