Skip to content

Commit

Permalink
Add inbox notification to parser (#158)
Browse files Browse the repository at this point in the history
* Add inbox notification to parser

* Add 1.11.0 TBA changelog
  • Loading branch information
Hopsaheysa authored Jun 20, 2024
1 parent 7312646 commit cca38ed
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 31 deletions.
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
7 changes: 6 additions & 1 deletion docs/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## 1.10.0
## 1.11.0 (TBA)

- Extended PushParser to support parsing of inbox notifications [(#158)](https://github.com/wultra/mtoken-sdk-ios/pull/158)
- Added statusReason to UserOperation [(#156)](https://github.com/wultra/mtoken-sdk-ios/pull/156)

## 1.10.0 (Apr 18, 2024)

- Removed `currentServerTime` property [(#148)](https://github.com/wultra/mtoken-sdk-android/pull/139)
- Added default and minimum pollingInterval [(#151)](https://github.com/wultra/mtoken-sdk-ios/pull/151)
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

0 comments on commit cca38ed

Please sign in to comment.