From 8966a0e22f712aeac354d6ef8725f51d308bbdcd Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 6 Jul 2022 16:06:35 +0100 Subject: [PATCH] ios: NSE waits up to 10 sec until app is suspended (#785) * ios: NSE waits up to 10 sec until app is suspended * show default notification if the app is no longer suspending/suspended --- .../ios/SimpleX NSE/NotificationService.swift | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/apps/ios/SimpleX NSE/NotificationService.swift b/apps/ios/SimpleX NSE/NotificationService.swift index 8decba34d1..dcee1bf91a 100644 --- a/apps/ios/SimpleX NSE/NotificationService.swift +++ b/apps/ios/SimpleX NSE/NotificationService.swift @@ -12,23 +12,37 @@ import SimpleXChat let logger = Logger() +let suspendingDelay: UInt64 = 2_000_000_000 + class NotificationService: UNNotificationServiceExtension { var contentHandler: ((UNNotificationContent) -> Void)? - var bestAttemptContent: UNMutableNotificationContent? + var bestAttemptContent: UNNotificationContent? override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { logger.debug("NotificationService.didReceive") - bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) + bestAttemptContent = request.content + self.contentHandler = contentHandler let appState = appStateGroupDefault.get() switch appState { case .suspended: logger.debug("NotificationService: app is suspended") - self.contentHandler = contentHandler receiveNtfMessages(request, contentHandler) case .suspending: logger.debug("NotificationService: app is suspending") - self.contentHandler = contentHandler - receiveNtfMessages(request, contentHandler) + Task { + var state = appState + for _ in 1...5 { + _ = try await Task.sleep(nanoseconds: suspendingDelay) + state = appStateGroupDefault.get() + if state == .suspended || state != .suspending { break } + } + logger.debug("NotificationService: app state is \(state.rawValue, privacy: .public)") + if state.inactive { + receiveNtfMessages(request, contentHandler) + } else { + contentHandler(request.content) + } + } default: logger.debug("NotificationService: app state is \(appState.rawValue, privacy: .public)") contentHandler(request.content)