Преглед на файлове

handle app icon badge always in notification manager; don't show a app icon badge for unread messages, if notifications are disabled

cyberta преди 4 години
родител
ревизия
61c66a572b
променени са 3 файла, в които са добавени 54 реда и са изтрити 35 реда
  1. 0 3
      DcCore/DcCore/DC/events.swift
  2. 1 1
      deltachat-ios/Coordinator/AppCoordinator.swift
  3. 53 31
      deltachat-ios/Helper/NotificationManager.swift

+ 0 - 3
DcCore/DcCore/DC/events.swift

@@ -104,9 +104,6 @@ public func handleEvent(event: DcEvent) {
                     "chat_id": Int(data1),
                 ]
             )
-
-            let array = DcContext.shared.getFreshMessages()
-            UIApplication.shared.applicationIconBadgeNumber = array.count
         }
 
     case DC_EVENT_CHAT_MODIFIED:

+ 1 - 1
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -108,7 +108,7 @@ class AppCoordinator {
         // the applicationIconBadgeNumber is remembered by the system even on reinstalls (just tested on ios 13.3.1),
         // to avoid appearing an old number of a previous installation, we reset the counter manually.
         // but even when this changes in ios, we need the reset as we allow account-deletion also in-app.
-        NotificationManager.updateApplicationIconBadge(reset: false)
+        NotificationManager.updateApplicationIconBadge(reset: true)
     }
 
     func presentTabBarController() {

+ 53 - 31
deltachat-ios/Helper/NotificationManager.swift

@@ -6,6 +6,8 @@ import UIKit
 public class NotificationManager {
     
     var incomingMsgObserver: Any?
+    var msgsNoticedObserver: Any?
+
 
     public static func updateApplicationIconBadge(reset: Bool) {
         if reset {
@@ -17,50 +19,70 @@ public class NotificationManager {
     }
 
     init() {
+        initIncomingMsgsObserver()
+        initMsgsNoticedObserver()
+    }
+    
+    private func initIncomingMsgsObserver() {
         incomingMsgObserver = NotificationCenter.default.addObserver(
             forName: dcNotificationIncoming,
             object: nil, queue: OperationQueue.main
         ) { notification in
             if let ui = notification.userInfo,
                let chatId = ui["chat_id"] as? Int,
-               let messageId = ui["message_id"] as? Int {
+               let messageId = ui["message_id"] as? Int,
+               !UserDefaults.standard.bool(forKey: "notifications_disabled") {
+                let array = DcContext.shared.getFreshMessages()
+                UIApplication.shared.applicationIconBadgeNumber = array.count
+                NotificationManager.updateApplicationIconBadge(reset: false)
+
                 let chat = DcContext.shared.getChat(chatId: chatId)
-                if !UserDefaults.standard.bool(forKey: "notifications_disabled") && !chat.isMuted {
-                    DispatchQueue.global(qos: .background).async {
-                        let content = UNMutableNotificationContent()
-                        let msg = DcMsg(id: messageId)
-                        content.title = chat.isGroup ? chat.name : msg.getSenderName(msg.fromContact)
-                        content.body =  msg.summary(chars: 80) ?? ""
-                        content.subtitle = chat.isGroup ?  msg.getSenderName(msg.fromContact) : ""
-                        content.userInfo = ui
-                        content.sound = .default
+                if chat.isMuted {
+                    return
+                }
 
-                        if msg.type == DC_MSG_IMAGE || msg.type == DC_MSG_GIF,
-                           let url = msg.fileURL {
-                            do {
-                                // make a copy of the file first since UNNotificationAttachment will move attached files into the attachment data store
-                                // so that they can be accessed by all of the appropriate processes
-                                let tempUrl = url.deletingLastPathComponent()
-                                    .appendingPathComponent("notification_tmp")
-                                    .appendingPathExtension(url.pathExtension)
-                                try FileManager.default.copyItem(at: url, to: tempUrl)
-                                if let attachment = try? UNNotificationAttachment(identifier: Constants.notificationIdentifier, url: tempUrl, options: nil) {
-                                    content.attachments = [attachment]
-                                }
-                            } catch let error {
-                                logger.error("Failed to copy file \(url) for notification preview generation: \(error)")
+                DispatchQueue.global(qos: .background).async {
+                    let content = UNMutableNotificationContent()
+                    let msg = DcMsg(id: messageId)
+                    content.title = chat.isGroup ? chat.name : msg.getSenderName(msg.fromContact)
+                    content.body =  msg.summary(chars: 80) ?? ""
+                    content.subtitle = chat.isGroup ?  msg.getSenderName(msg.fromContact) : ""
+                    content.userInfo = ui
+                    content.sound = .default
+
+                    if msg.type == DC_MSG_IMAGE || msg.type == DC_MSG_GIF,
+                       let url = msg.fileURL {
+                        do {
+                            // make a copy of the file first since UNNotificationAttachment will move attached files into the attachment data store
+                            // so that they can be accessed by all of the appropriate processes
+                            let tempUrl = url.deletingLastPathComponent()
+                                .appendingPathComponent("notification_tmp")
+                                .appendingPathExtension(url.pathExtension)
+                            try FileManager.default.copyItem(at: url, to: tempUrl)
+                            if let attachment = try? UNNotificationAttachment(identifier: Constants.notificationIdentifier, url: tempUrl, options: nil) {
+                                content.attachments = [attachment]
                             }
+                        } catch let error {
+                            logger.error("Failed to copy file \(url) for notification preview generation: \(error)")
                         }
-                        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
-
-                        let request = UNNotificationRequest(identifier: Constants.notificationIdentifier, content: content, trigger: trigger)
-                        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
-                        DcContext.shared.logger?.info("notifications: added \(content.title) \(content.body) \(content.userInfo)")
                     }
+                    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
+
+                    let request = UNNotificationRequest(identifier: Constants.notificationIdentifier, content: content, trigger: trigger)
+                    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
+                    DcContext.shared.logger?.info("notifications: added \(content.title) \(content.body) \(content.userInfo)")
                 }
+            }
+        }
+    }
 
-                let array = DcContext.shared.getFreshMessages()
-                UIApplication.shared.applicationIconBadgeNumber = array.count
+    private func initMsgsNoticedObserver() {
+        msgsNoticedObserver =  NotificationCenter.default.addObserver(
+            forName: dcNotificationIncoming,
+            object: nil, queue: OperationQueue.main
+        ) { _ in
+            if !UserDefaults.standard.bool(forKey: "notifications_disabled"){
+                NotificationManager.updateApplicationIconBadge(reset: false)
             }
         }
     }