From 9542b0e0f96f658afa2f6ac8b0f62149ff31683d Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Thu, 21 Apr 2022 10:34:50 +1000 Subject: [PATCH] add CallKit supported filter for NSE --- SessionMessagingKit/Database/SSKPreferences.swift | 14 ++++++++++++++ .../NotificationServiceExtension.swift | 11 +++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/SessionMessagingKit/Database/SSKPreferences.swift b/SessionMessagingKit/Database/SSKPreferences.swift index ea6114f42..5897e08ce 100644 --- a/SessionMessagingKit/Database/SSKPreferences.swift +++ b/SessionMessagingKit/Database/SSKPreferences.swift @@ -37,6 +37,20 @@ public class SSKPreferences: NSObject { setBool(newValue, key: areCallsEnabledKey) } } + + @objc + public static var isCallKitSupported() -> Bool { + let userLocale = NSLocale.current + + guard let regionCode = userLocale.regionCode else { return false } + + if regionCode.contains("CN") || + regionCode.contains("CHN") { + return false + } else { + return true + } + } // MARK: - diff --git a/SessionNotificationServiceExtension/NotificationServiceExtension.swift b/SessionNotificationServiceExtension/NotificationServiceExtension.swift index aeec50602..db7e22d51 100644 --- a/SessionNotificationServiceExtension/NotificationServiceExtension.swift +++ b/SessionNotificationServiceExtension/NotificationServiceExtension.swift @@ -203,7 +203,7 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension } private func handleSuccessForIncomingCall(for callMessage: CallMessage, using transaction: YapDatabaseReadWriteTransaction) { - if #available(iOSApplicationExtension 14.5, *) { + if #available(iOSApplicationExtension 14.5, *), SSKPreferences.isCallKitSupported { if let uuid = callMessage.uuid, let caller = callMessage.sender, let timestamp = callMessage.sentTimestamp { let payload: JSON = ["uuid": uuid, "caller": caller, "timestamp": timestamp] CXProvider.reportNewIncomingVoIPPushPayload(payload) { error in @@ -239,8 +239,15 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension } let identifier = self.request?.identifier ?? UUID().uuidString let request = UNNotificationRequest(identifier: identifier, content: notificationContent, trigger: nil) + 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("Add remote notification request") - UNUserNotificationCenter.current().add(request) } private func handleSuccess(for content: UNMutableNotificationContent) {