Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inbox notification to parser #158

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 54 additions & 29 deletions WultraMobileTokenSDK/Push/WMTPushParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion WultraMobileTokenSDKTests/PushParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions docs/Using-Push-Service.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Loading