From 7c579ca634cc5686b78c6e040a8a87615b031fe4 Mon Sep 17 00:00:00 2001 From: Marek Stransky Date: Fri, 14 Jun 2024 12:07:29 +0200 Subject: [PATCH] Add inbox notification to parser --- WultraMobileTokenSDK/Push/WMTPushParser.swift | 83 ++++++++++++------- .../PushParserTests.swift | 7 +- docs/Using-Push-Service.md | 3 + 3 files changed, 63 insertions(+), 30 deletions(-) diff --git a/WultraMobileTokenSDK/Push/WMTPushParser.swift b/WultraMobileTokenSDK/Push/WMTPushParser.swift index 097474d..4e13607 100644 --- a/WultraMobileTokenSDK/Push/WMTPushParser.swift +++ b/WultraMobileTokenSDK/Push/WMTPushParser.swift @@ -33,44 +33,66 @@ public class WMTPushParser { /// - Parameter userInfo: user info of received notification /// - Returns: returns a parsed known WMTPushMessage or nil public static func parseNotification(_ userInfo: [AnyHashable: Any]) -> WMTPushMessage? { - - guard let messageType = userInfo["messageType"] as? String, - let operationId = userInfo["operationId"] as? String, - let operationName = userInfo["operationName"] as? String else { - return nil + guard let messageType = userInfo["messageType"] as? String else { + return nil } switch messageType { case "mtoken.operationInit": - - var content: WMTPushContent? - - if let alert = (userInfo["aps"] as? NSDictionary)?["alert"] as? NSDictionary, - let title = alert["title"] as? String, - let body = alert["body"] as? String { - - content = (title, body) - } - - return .operationCreated(id: operationId, name: operationName, content: content, originalData: userInfo) + return parseOperationCreated(userInfo) case "mtoken.operationFinished": - guard let result = userInfo["mtokenOperationResult"] as? String else { - return nil - } - let opResult: WMTPushOperationFinishedResult - switch result { - case "authentication.success": opResult = .success - case "authentication.fail": opResult = .fail - case "operation.timeout": opResult = .timeout - case "operation.canceled": opResult = .canceled - case "operation.methodNotAvailable": opResult = .methodNotAvailable - default: opResult = .unknown // to be forward compatible - } - return .operationFinished(id: operationId, name: operationName, result: opResult, originalData: userInfo) + return parseOperationFinished(userInfo) + case "mtoken.inboxMessage.new": + return parseInboxMessage(userInfo) default: return nil } } + + // Helper methods + private static func parseOperationCreated(_ userInfo: [AnyHashable: Any]) -> WMTPushMessage? { + guard let operationId = userInfo["operationId"] as? String, + let operationName = userInfo["operationName"] as? String else { + return nil + } + + var content: WMTPushContent? + + if let alert = (userInfo["aps"] as? NSDictionary)?["alert"] as? NSDictionary, + let title = alert["title"] as? String, + let body = alert["body"] as? String { + content = (title, body) + } + + return .operationCreated(id: operationId, name: operationName, content: content, originalData: userInfo) + } + + private static func parseOperationFinished(_ userInfo: [AnyHashable: Any]) -> WMTPushMessage? { + guard let operationId = userInfo["operationId"] as? String, + let operationName = userInfo["operationName"] as? String, + let result = userInfo["mtokenOperationResult"] as? String else { + return nil + } + + let opResult: WMTPushOperationFinishedResult + switch result { + case "authentication.success": opResult = .success + case "authentication.fail": opResult = .fail + case "operation.timeout": opResult = .timeout + case "operation.canceled": opResult = .canceled + case "operation.methodNotAvailable": opResult = .methodNotAvailable + default: opResult = .unknown // to be forward compatible + } + + return .operationFinished(id: operationId, name: operationName, result: opResult, originalData: userInfo) + } + + private static func parseInboxMessage(_ userInfo: [AnyHashable: Any]) -> WMTPushMessage? { + guard let inboxId = userInfo["inboxId"] as? String else { + return nil + } + return .inboxMessageReceived(id: inboxId, originalData: userInfo) + } } /// Known push message. @@ -80,6 +102,9 @@ public enum WMTPushMessage { /// An operation was finished, successfully or non-successfully. case operationFinished(id: String, name: String, result: WMTPushOperationFinishedResult, originalData: [AnyHashable: Any]) + + /// A new inbox message was triggered. + case inboxMessageReceived(id: String, originalData: [AnyHashable: Any]) } /// Action which finished the operation. diff --git a/WultraMobileTokenSDKTests/PushParserTests.swift b/WultraMobileTokenSDKTests/PushParserTests.swift index 17ca9a0..b049d3a 100644 --- a/WultraMobileTokenSDKTests/PushParserTests.swift +++ b/WultraMobileTokenSDKTests/PushParserTests.swift @@ -118,13 +118,18 @@ class PushParserTests: XCTestCase { XCTAssertNil(makePush(type: "mtoken.operationFinished", id: "1", name: nil, title: nil, message: nil, opResult: nil)) } + func testInboxNewMessage() { + XCTAssertNotNil(makePush(type: "mtoken.inboxMessage.new", id: nil, name: nil, title: nil, message: nil, opResult: nil, inboxId: "666")) + } + // helper methods - private func makePush(type: String?, id: String?, name: String?, title: String?, message: String?, opResult: String?) -> WMTPushMessage? { + private func makePush(type: String?, id: String?, name: String?, title: String?, message: String?, opResult: String?, inboxId: String? = nil) -> WMTPushMessage? { var userInfo = [AnyHashable: Any]() userInfo["messageType"] = type userInfo["operationId"] = id userInfo["operationName"] = name + userInfo["inboxId"] = inboxId userInfo["aps"] = ["alert": ["title": title, "body": message]] userInfo["mtokenOperationResult"] = opResult return WMTPushParser.parseNotification(userInfo) diff --git a/docs/Using-Push-Service.md b/docs/Using-Push-Service.md index 1065b93..e98677d 100644 --- a/docs/Using-Push-Service.md +++ b/docs/Using-Push-Service.md @@ -88,6 +88,9 @@ The `WMTPushMessage` can be following values - `name` of the operation - `result` of the operation (for example that the operation was canceled by the user). - `originalData` - data on which was the push message constructed +- `inboxMessageReceived` - a new inbox message was triggered. + - `id` of the message + - `originalData` - data on which was the push message constructed Example push notification processing: