TF-2384 Handle NSE to get plain notification in iOS

Signed-off-by: dab246 <tdvu@linagora.com>
(cherry picked from commit 364f32a14b9888f73a3a103b8b5e1bc9b31e148e)
This commit is contained in:
dab246
2023-12-24 20:09:25 +07:00
committed by Dat H. Pham
parent edcc30808c
commit a1f86d502a
18 changed files with 932 additions and 73 deletions
@@ -0,0 +1,16 @@
import Foundation
public extension Bundle {
/// The top-level bundle that contains the entire app.
static var app: Bundle {
var bundle = Bundle.main
if bundle.bundleURL.pathExtension == "appex" {
// Peel off two directory levels - MY_APP.app/PlugIns/MY_APP_EXTENSION.appex
let url = bundle.bundleURL.deletingLastPathComponent().deletingLastPathComponent()
if let otherBundle = Bundle(url: url) {
bundle = otherBundle
}
}
return bundle
}
}
@@ -0,0 +1,45 @@
import Foundation
struct InfoPlistReader {
private enum Keys {
static let baseBundleIdentifier = "baseBundleIdentifier"
static let keychainAccessGroupIdentifier = "keychainAccessGroupIdentifier"
static let bundleDisplayName = "CFBundleDisplayName"
}
/// Info.plist reader on the bundle object that contains the current executable.
static let main = InfoPlistReader(bundle: .main)
/// Info.plist reader on the bundle object that contains the main app executable.
static let app = InfoPlistReader(bundle: .app)
private let bundle: Bundle
/// Initializer
/// - Parameter bundle: bundle to read values from
init(bundle: Bundle) {
self.bundle = bundle
}
/// Base bundle identifier set in Info.plist of the target
var baseBundleIdentifier: String {
infoPlistValue(forKey: Keys.baseBundleIdentifier)
}
/// Keychain access group identifier set in Info.plist of the target
var keychainAccessGroupIdentifier: String {
infoPlistValue(forKey: Keys.keychainAccessGroupIdentifier)
}
/// Bundle display name of the target
var bundleDisplayName: String {
infoPlistValue(forKey: Keys.bundleDisplayName)
}
private func infoPlistValue<T>(forKey key: String) -> T {
guard let result = bundle.object(forInfoDictionaryKey: key) as? T else {
fatalError("Add \(key) into your target's Info.plst")
}
return result
}
}
@@ -0,0 +1,37 @@
import Foundation
class PayloadParser {
static let shared: PayloadParser = PayloadParser()
private let prefixState: String = ":"
private func validatePushNotificationStateChange(state: String) -> Bool {
return state.contains(prefixState) &&
(state.contains(TypeName.mailbox.rawValue) ||
state.contains(TypeName.email.rawValue) ||
state.contains(TypeName.EmailDelivery.rawValue))
}
func parsingPayloadNotification(payloadData: [String: Any]) -> [String: [TypeName: String]]{
var mapStateChanges = [String: [TypeName: String]]()
payloadData.keys.forEach { key in
if validatePushNotificationStateChange(state: key),
let accountId = key.components(separatedBy: prefixState).first,
let typeName = TypeName(rawValue: key.components(separatedBy: prefixState).last ?? ""),
let stateValue = payloadData[key] as? String {
if (mapStateChanges.keys.contains(accountId)) {
var mapTypes = mapStateChanges[accountId]!
mapTypes[typeName] = stateValue
mapStateChanges[accountId] = mapTypes
} else {
var mapTypes = [TypeName: String]()
mapTypes[typeName] = stateValue
mapStateChanges[accountId] = mapTypes
}
}
}
return mapStateChanges
}
}