From 8652268557f3393f8c639322f335dbb3fc8cc83a Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Mon, 21 Mar 2022 13:55:51 +1100 Subject: [PATCH] add logs & fix PN --- Session/Shared/BaseVC.swift | 3 +++ .../Pollers/OpenGroupPollerV2.swift | 1 + .../NSENotificationPresenter.swift | 20 +++++++++++++------ .../NotificationServiceExtension.swift | 5 ++++- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Session/Shared/BaseVC.swift b/Session/Shared/BaseVC.swift index b2953ed36..0bccafb10 100644 --- a/Session/Shared/BaseVC.swift +++ b/Session/Shared/BaseVC.swift @@ -106,6 +106,9 @@ class BaseVC : UIViewController { } override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { + if #available(iOS 13.0, *) { + SNLog("Current trait collection: \(UITraitCollection.current), previous trait collection: \(previousTraitCollection)") + } if LKAppModeUtilities.isSystemDefault { NotificationCenter.default.post(name: .appModeChanged, object: nil) } diff --git a/SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupPollerV2.swift b/SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupPollerV2.swift index 4c2ddbc8a..a1b3a47b3 100644 --- a/SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupPollerV2.swift +++ b/SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupPollerV2.swift @@ -48,6 +48,7 @@ public final class OpenGroupPollerV2 : NSObject { guard let self = self else { return } self.isPolling = false bodies.forEach { self.handleCompactPollBody($0, isBackgroundPoll: isBackgroundPoll) } + SNLog("Open group polling finished for \(self.server).") seal.fulfill(()) }.catch(on: OpenGroupAPIV2.workQueue) { error in SNLog("Open group polling failed due to error: \(error).") diff --git a/SessionNotificationServiceExtension/NSENotificationPresenter.swift b/SessionNotificationServiceExtension/NSENotificationPresenter.swift index 6e330b94a..836f54556 100644 --- a/SessionNotificationServiceExtension/NSENotificationPresenter.swift +++ b/SessionNotificationServiceExtension/NSENotificationPresenter.swift @@ -13,14 +13,14 @@ public class NSENotificationPresenter: NSObject, NotificationsProtocol { // to check if this is the only message request thread (group threads can't be message requests // so just ignore those and if the user has hidden message requests then we want to show the // notification regardless of how many message requests there are) - if !thread.isGroupThread() && thread.isMessageRequest() && !CurrentAppContext().appUserDefaults()[.hasHiddenMessageRequests] { + if !thread.isGroupThread() && thread.isMessageRequest(using: transaction) && !CurrentAppContext().appUserDefaults()[.hasHiddenMessageRequests] { let threads = transaction.ext(TSThreadDatabaseViewExtensionName) as! YapDatabaseViewTransaction let numMessageRequests = threads.numberOfItems(inGroup: TSMessageRequestGroup) // Allow this to show a notification if there are no message requests (ie. this is the first one) guard numMessageRequests == 0 else { return } } - else if thread.isMessageRequest() && CurrentAppContext().appUserDefaults()[.hasHiddenMessageRequests] { + else if thread.isMessageRequest(using: transaction) && CurrentAppContext().appUserDefaults()[.hasHiddenMessageRequests] { // If there are other interactions on this thread already then don't show the notification if thread.numberOfInteractions(with: transaction) > 1 { return } @@ -37,7 +37,7 @@ public class NSENotificationPresenter: NSObject, NotificationsProtocol { } let context = Contact.context(for: thread) - let senderName = Storage.shared.getContact(with: senderPublicKey)?.displayName(for: context) ?? senderPublicKey + let senderName = Storage.shared.getContact(with: senderPublicKey, using: transaction)?.displayName(for: context) ?? senderPublicKey var notificationTitle = senderName if let group = thread as? TSGroupThread { @@ -85,7 +85,7 @@ public class NSENotificationPresenter: NSObject, NotificationsProtocol { // If it's a message request then overwrite the body to be something generic (only show a notification // when receiving a new message request if there aren't any others or the user had hidden them) - if thread.isMessageRequest() { + if thread.isMessageRequest(using: transaction) { notificationContent.title = "Session" notificationContent.body = "MESSAGE_REQUESTS_NOTIFICATION".localized() } @@ -93,8 +93,16 @@ public class NSENotificationPresenter: NSObject, NotificationsProtocol { // Add request let identifier = incomingMessage.notificationIdentifier ?? UUID().uuidString let request = UNNotificationRequest(identifier: identifier, content: notificationContent, trigger: nil) - SNLog("Add remote notification request") - UNUserNotificationCenter.current().add(request) + SNLog("Add remote notification request: \(notificationContent.body)") + let semaphore = DispatchSemaphore(value: 0) + UNUserNotificationCenter.current().add(request) { error in + if let error = error { + SNLog("Failed to add notification request due to error:\(error)") + } + semaphore.signal() + } + semaphore.wait() + SNLog("Finish adding remote notification request") } public func cancelNotification(_ identifier: String) { diff --git a/SessionNotificationServiceExtension/NotificationServiceExtension.swift b/SessionNotificationServiceExtension/NotificationServiceExtension.swift index d35de1ffe..c78342b43 100644 --- a/SessionNotificationServiceExtension/NotificationServiceExtension.swift +++ b/SessionNotificationServiceExtension/NotificationServiceExtension.swift @@ -41,7 +41,10 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension let envelope = try? MessageWrapper.unwrap(data: data), let envelopeAsData = try? envelope.serializedData() else { return self.handleFailure(for: notificationContent) } - Storage.write { transaction in // Intentionally capture self + // HACK: It is important to use writeSync() here to avoid a race condition + // where the completeSilenty() is called before the local notification request + // is added to notification center. + Storage.writeSync { transaction in // Intentionally capture self do { let (message, proto) = try MessageReceiver.parse(envelopeAsData, openGroupMessageServerID: nil, using: transaction) switch message {