Răsfoiți Sursa

Merge pull request #1120 from deltachat/foreground_notifications

No foreground notifications for opened chat
bjoern 4 ani în urmă
părinte
comite
f1626fe9a6

+ 4 - 4
deltachat-ios/AppDelegate.swift

@@ -425,10 +425,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
     // MARK: - Handle notification banners
 
-    // this method will be called if an incoming message was received while the app was in forground.
-    // on iOS 14+ we show a notification in the notification center, without triggering an alert that needs to be manually swiped away
-    // on pre iOS 14 this feature isn't available so we're just triggering to show the badge at the launcher icon
-    func userNotificationCenter(_: UNUserNotificationCenter, willPresent _: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
+    // This method will be called if an incoming message was received while the app was in forground.
+    // On iOS 14+ we show a non-intrusive notification in the notification center, if the notification doesn't belong to a currently opened chat.
+    // On pre iOS 14 this feature isn't available so we're just triggering to show the badge at the launcher icon. This will only have an effect for unread messages > 0
+    func userNotificationCenter(_: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
         logger.info("forground notification")
         if #available(iOS 14.0, *) {
             completionHandler([.list, .badge])

+ 8 - 2
deltachat-ios/Helper/NotificationManager.swift

@@ -22,7 +22,7 @@ public class NotificationManager {
         initIncomingMsgsObserver()
         initMsgsNoticedObserver()
     }
-    
+
     private func initIncomingMsgsObserver() {
         incomingMsgObserver = NotificationCenter.default.addObserver(
             forName: dcNotificationIncoming,
@@ -33,6 +33,12 @@ public class NotificationManager {
                    let chatId = ui["chat_id"] as? Int,
                    let messageId = ui["message_id"] as? Int,
                    !UserDefaults.standard.bool(forKey: "notifications_disabled") {
+                    
+                    if let lastChatId = AppStateRestorer.shared.restoreLastActiveChatId(),
+                       lastChatId == chatId {
+                        return
+                    }
+
                     let array = DcContext.shared.getFreshMessages()
                     UIApplication.shared.applicationIconBadgeNumber = array.count
                     NotificationManager.updateApplicationIconBadge(reset: false)
@@ -78,7 +84,7 @@ public class NotificationManager {
 
     private func initMsgsNoticedObserver() {
         msgsNoticedObserver =  NotificationCenter.default.addObserver(
-            forName: dcNotificationIncoming,
+            forName: dcMsgsNoticed,
             object: nil, queue: OperationQueue.main
         ) { _ in
             DispatchQueue.global(qos: .background).async {