Skip to content

Commit 9d603c7

Browse files
Merge pull request #56 from Iterable/feature/mob-211-payload
[MOB-211] - move custom payload element from content/payload => customPayload
2 parents 07c13b3 + 6f8ca69 commit 9d603c7

File tree

6 files changed

+107
-9
lines changed

6 files changed

+107
-9
lines changed

Tests/swift-sdk-swift-tests/InAppHelperTests.swift

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,110 @@ class InAppHelperTests: XCTestCase {
303303
let dict = messages[0].trigger.dict as! [String : Any]
304304
TestUtils.validateMatch(keyPath: KeyPath("myPayload.var1"), value: "val1", inDictionary: dict, message: "Expected to find val1")
305305
}
306+
307+
// This tests payload as we expect to get when backend is fixed
308+
func testInAppPayloadParsing() {
309+
let customPayload1 = """
310+
{
311+
"contentType" : "html",
312+
"inAppType" : "default",
313+
"channelName" : "channel1"
314+
}
315+
"""
316+
let customPayload2 = """
317+
{
318+
"contentType" : "html",
319+
"inAppType" : "inBox",
320+
"channelName" : "channel2",
321+
"promoteToContent" : {
322+
"title" : "title",
323+
"subTitle" : "subtitle",
324+
"imageUrl" : "http://somewhere.com/something.jpg"
325+
}
326+
}
327+
"""
328+
329+
let payload = """
330+
{
331+
"inAppMessages" : [
332+
{
333+
"content" : {
334+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
335+
},
336+
"messageId" : "messageIdxxx",
337+
"campaignId" : "campaignIdxxx",
338+
"trigger" : {
339+
"type" : "myNewKind",
340+
"myPayload" : {"var1" : "val1"}
341+
},
342+
"customPayload" : \(customPayload1)
343+
},
344+
{
345+
"content" : {
346+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
347+
},
348+
"messageId" : "messageIdxxx",
349+
"campaignId" : "campaignIdxxx",
350+
"trigger" : {
351+
"type" : "myNewKind",
352+
"myPayload" : {"var1" : "val1"}
353+
},
354+
"customPayload" : \(customPayload2)
355+
},
356+
{
357+
"content" : {
358+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
359+
},
360+
"messageId" : "messageIdxxx",
361+
"campaignId" : "campaignIdxxx",
362+
"trigger" : {
363+
"type" : "myNewKind",
364+
"myPayload" : {"var1" : "val1"}
365+
},
366+
"customPayload" : {}
367+
},
368+
{
369+
"content" : {
370+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
371+
},
372+
"messageId" : "messageIdxxx",
373+
"campaignId" : "campaignIdxxx",
374+
"trigger" : {
375+
"type" : "myNewKind",
376+
"myPayload" : {"var1" : "val1"}
377+
}
378+
}
379+
]
380+
}
381+
""".toJsonDict()
382+
let messages = InAppHelper.inAppMessages(fromPayload: payload, internalApi: IterableAPI.internalImplementation!)
383+
384+
XCTAssertEqual(messages.count, 4)
385+
let message1 = messages[0]
386+
XCTAssertEqual(message1.channelName, "channel1")
387+
XCTAssertTrue(TestUtils.areEqual(dict1: message1.extraInfo!, dict2: customPayload1.toJsonDict()))
388+
389+
let message2 = messages[1]
390+
XCTAssertEqual(message2.channelName, "channel2")
391+
XCTAssertTrue(TestUtils.areEqual(dict1: message2.extraInfo!, dict2: customPayload2.toJsonDict()))
392+
393+
let message3 = messages[2]
394+
XCTAssertEqual(message3.channelName, "")
395+
396+
let message4 = messages[3]
397+
XCTAssertEqual(message4.channelName, "")
398+
}
306399

307400
private func createInAppPayload(withExtraInfo extraInfo: [AnyHashable : Any]) -> [AnyHashable : Any] {
308401
return [
309402
"inAppMessages" : [[
310403
"content" : [
311404
"html" : "<a href='href1'>Click Here</a>",
312405
"inAppDisplaySettings" : ["backgroundAlpha" : 0.5, "left" : ["percentage" : 60], "right" : ["percentage" : 60], "bottom" : ["displayOption" : "AutoExpand"], "top" : ["displayOption" : "AutoExpand"]],
313-
"payload" : extraInfo
314406
],
315407
"messageId" : "messageIdxxx",
316408
"campaignId" : "campaignIdxxx",
409+
"customPayload" : extraInfo
317410
]]
318411
]
319412
}

Tests/swift-sdk-swift-tests/TestInAppPayloadGenerator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ struct TestInAppPayloadGenerator {
8181
"content" : [
8282
"html" : "<a href='\(href)'>Click Here</a>",
8383
"inAppDisplaySettings" : ["backgroundAlpha" : 0.5, "left" : ["percentage" : 60], "right" : ["percentage" : 60], "bottom" : ["displayOption" : "AutoExpand"], "top" : ["displayOption" : "AutoExpand"]],
84-
"payload" : ["channelName" : "inBox", "title" : "Product 1 Available", "date" : "2018-11-14T14:00:00:00.32Z"]
8584
],
8685
"messageId" : getMessageId(index: index),
87-
"campaignId" : getCampaignId(index: index)
86+
"campaignId" : getCampaignId(index: index),
87+
"customPayload" : ["channelName" : "inBox", "title" : "Product 1 Available", "date" : "2018-11-14T14:00:00:00.32Z"]
8888
]
8989
}
9090

Tests/swift-sdk-swift-tests/TestUtils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ struct TestUtils {
6060
return TestHelper.clearTestUserDefaults()
6161
}
6262

63+
static func areEqual(dict1: [AnyHashable : Any], dict2: [AnyHashable : Any]) -> Bool {
64+
return NSDictionary(dictionary: dict1).isEqual(to: dict2)
65+
}
66+
6367
private static func validateQueryParameters(inUrlComponents urlComponents: URLComponents, queryParams: [(name:String, value:String)]) {
6468
queryParams.forEach { (name, value) in
6569
validateQueryParameter(inUrlComponents: urlComponents, withName: name, andValue: value)

swift-sdk/ITBConsts.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public extension AnyHashable {
156156
public static let ITBL_IN_APP_HTML = "html"
157157
public static let ITBL_IN_APP_HREF = "href"
158158
public static let ITBL_IN_APP_DISPLAY_SETTINGS = "inAppDisplaySettings"
159-
public static let ITBL_IN_APP_PAYLOAD = "payload"
159+
public static let ITBL_IN_APP_CUSTOM_PAYLOAD = "customPayload"
160+
public static let ITBL_IN_APP_CHANNEL_NAME = "channelName"
160161
}
161162

swift-sdk/Internal/InAppHelper.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ struct InAppHelper {
327327
parseInApp(fromDict: $0)
328328
}
329329
}
330-
330+
331331
private static func parseInApp(fromDict dict: [AnyHashable : Any]) -> InAppParseResult {
332332
guard let content = dict[.ITBL_IN_APP_CONTENT] as? [AnyHashable : Any] else {
333333
return .failure(reason: "no message", messageId: nil)
@@ -350,7 +350,7 @@ struct InAppHelper {
350350
campaignId = ""
351351
}
352352

353-
let extraInfo = parseExtraInfo(fromContent: content)
353+
let extraInfo = parseCustomPayload(fromPayload: dict)
354354

355355
// this is temporary until we fix backend
356356
let channelName = extraInfo?["channelName"] as? String ?? ""
@@ -390,8 +390,8 @@ struct InAppHelper {
390390
return IterableInAppTrigger(dict: element)
391391
}
392392

393-
private static func parseExtraInfo(fromContent content: [AnyHashable : Any]) -> [AnyHashable : Any]? {
394-
return content[.ITBL_IN_APP_PAYLOAD] as? [AnyHashable : Any]
393+
private static func parseCustomPayload(fromPayload payload: [AnyHashable : Any]) -> [AnyHashable : Any]? {
394+
return payload[.ITBL_IN_APP_CUSTOM_PAYLOAD] as? [AnyHashable : Any]
395395
}
396396

397397
private static func toMessage(fromInAppParseResult inAppParseResult: InAppHelper.InAppParseResult, internalApi: IterableAPIInternal) -> IterableInAppMessage? {

swift-sdk/IterableInApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public final class IterableInAppMessage : NSObject {
144144
init(
145145
messageId: String,
146146
campaignId: String,
147-
channelName: String = "reserved",
147+
channelName: String = "",
148148
contentType: IterableInAppContentType = .html,
149149
trigger: IterableInAppTrigger = .defaultTrigger,
150150
expiresAt: Date? = nil,

0 commit comments

Comments
 (0)