Skip to content

Commit 2633c5c

Browse files
Merge pull request #503 from Iterable/tapash/mob-3363-change-action-icon-payload
[MOB-3363] - Change action icon payload
2 parents 3e7dd31 + 34225d3 commit 2633c5c

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

notification-extension/NotificationContentParser.swift

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ struct NotificationContentParser {
6060
}
6161
} else {
6262
if #available(iOS 15.0, *) {
63-
return UNTextInputNotificationAction(identifier: button.identifier,
64-
title: button.title,
65-
options: getOptions(forActionButton: button),
66-
icon: getNotificationIcon(forActionButton: button))
63+
return UNNotificationAction(identifier: button.identifier,
64+
title: button.title,
65+
options: getOptions(forActionButton: button),
66+
icon: getNotificationIcon(forActionButton: button))
6767
} else {
68-
return UNTextInputNotificationAction(identifier: button.identifier,
69-
title: button.title,
70-
options: getOptions(forActionButton: button))
68+
return UNNotificationAction(identifier: button.identifier,
69+
title: button.title,
70+
options: getOptions(forActionButton: button))
7171
}
7272
}
7373
}
@@ -76,15 +76,15 @@ struct NotificationContentParser {
7676
guard let identifier = json[JsonKey.ActionButton.identifier] as? String else { return nil }
7777
guard let title = json[JsonKey.ActionButton.title] as? String else { return nil }
7878

79+
let actionIcon = (json[JsonKey.ActionButton.actionIcon] as? [AnyHashable: Any]).flatMap(ActionIcon.from(json:))
7980
return ActionButton(identifier: identifier,
8081
title: title,
8182
buttonType: getButtonType(info: json),
8283
openApp: getBoolValue(json[JsonKey.ActionButton.openApp]) ?? true,
8384
requiresUnlock: getBoolValue(json[JsonKey.ActionButton.requiresUnlock]) ?? false,
8485
textInputTitle: json[JsonKey.ActionButton.inputTitle] as? String,
8586
textInputPlaceholder: json[JsonKey.ActionButton.inputPlaceholder] as? String,
86-
systemImageName: json[JsonKey.ActionButton.systemImageName] as? String,
87-
templateImageName: json[JsonKey.ActionButton.templateImageName] as? String)
87+
actionIcon: actionIcon)
8888
}
8989

9090
private static func getButtonType(info: [AnyHashable: Any]) -> ButtonType {
@@ -120,13 +120,14 @@ struct NotificationContentParser {
120120

121121
@available(iOS 15.0, *)
122122
private static func getNotificationIcon(forActionButton button: ActionButton) -> UNNotificationActionIcon? {
123-
if let systemImageName = button.systemImageName {
124-
return UNNotificationActionIcon(systemImageName: systemImageName)
125-
} else if let templateImageName = button.templateImageName {
126-
return UNNotificationActionIcon(templateImageName: templateImageName)
127-
} else {
123+
guard let actionIcon = button.actionIcon else {
128124
return nil
129125
}
126+
if actionIcon.iconType == .systemImage {
127+
return UNNotificationActionIcon(systemImageName: actionIcon.imageName)
128+
} else {
129+
return UNNotificationActionIcon(templateImageName: actionIcon.imageName)
130+
}
130131
}
131132

132133
private enum ButtonType: String {
@@ -143,7 +144,24 @@ struct NotificationContentParser {
143144
let requiresUnlock: Bool
144145
let textInputTitle: String?
145146
let textInputPlaceholder: String?
146-
let systemImageName: String?
147-
let templateImageName: String?
147+
let actionIcon: ActionIcon?
148+
}
149+
150+
private enum ActionIconType: String, Codable {
151+
case systemImage
152+
case templateImage
153+
}
154+
155+
private struct ActionIcon: Codable {
156+
let iconType: ActionIconType
157+
let imageName: String
158+
159+
static func from(json: [AnyHashable: Any]) -> ActionIcon? {
160+
guard let data = try? JSONSerialization.data(withJSONObject: json, options: []) else {
161+
return nil
162+
}
163+
return try? JSONDecoder().decode(ActionIcon.self, from: data)
164+
}
148165
}
149166
}
167+

notification-extension/NotificationExtensionConstants.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public enum JsonKey {
1313
static let requiresUnlock = "requiresUnlock"
1414
static let inputTitle = "inputTitle"
1515
static let inputPlaceholder = "inputPlaceholder"
16-
static let systemImageName = "systemImageName"
17-
static let templateImageName = "templateImageName"
16+
static let actionIcon = "actionIcon"
1817
}
1918

2019
public enum Payload {

swift-sdk.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
AC845107228DF54E0052BB8F /* ApiClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC845106228DF54E0052BB8F /* ApiClient.swift */; };
136136
AC84510922910A0C0052BB8F /* RequestCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC84510822910A0C0052BB8F /* RequestCreator.swift */; };
137137
AC84EC3726C136F3007FBEF7 /* NotificationContentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC84EC3626C136F3007FBEF7 /* NotificationContentParser.swift */; };
138-
AC85A749216D24F4005241AE /* NotificationExtensionSwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC85A748216D24F4005241AE /* NotificationExtensionSwiftTests.swift */; };
138+
AC85A749216D24F4005241AE /* NotificationExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC85A748216D24F4005241AE /* NotificationExtensionTests.swift */; };
139139
AC87172621A4E47E00FEA369 /* TestInAppPayloadGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC87172521A4E47E00FEA369 /* TestInAppPayloadGenerator.swift */; };
140140
AC8874AA22178BD80075B54B /* InAppContentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC8874A922178BD80075B54B /* InAppContentParser.swift */; };
141141
AC89661E2124FBCE0051A6CD /* IterableAutoRegistrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC89661D2124FBCE0051A6CD /* IterableAutoRegistrationTests.swift */; };
@@ -491,7 +491,7 @@
491491
AC845106228DF54E0052BB8F /* ApiClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiClient.swift; sourceTree = "<group>"; };
492492
AC84510822910A0C0052BB8F /* RequestCreator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestCreator.swift; sourceTree = "<group>"; };
493493
AC84EC3626C136F3007FBEF7 /* NotificationContentParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationContentParser.swift; sourceTree = "<group>"; };
494-
AC85A748216D24F4005241AE /* NotificationExtensionSwiftTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationExtensionSwiftTests.swift; sourceTree = "<group>"; };
494+
AC85A748216D24F4005241AE /* NotificationExtensionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationExtensionTests.swift; sourceTree = "<group>"; };
495495
AC87172521A4E47E00FEA369 /* TestInAppPayloadGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestInAppPayloadGenerator.swift; sourceTree = "<group>"; };
496496
AC8874A922178BD80075B54B /* InAppContentParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InAppContentParser.swift; sourceTree = "<group>"; };
497497
AC89661D2124FBCE0051A6CD /* IterableAutoRegistrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IterableAutoRegistrationTests.swift; sourceTree = "<group>"; };
@@ -1082,7 +1082,7 @@
10821082
isa = PBXGroup;
10831083
children = (
10841084
AC0674E720D87D5B00C2806D /* Helper Files */,
1085-
AC85A748216D24F4005241AE /* NotificationExtensionSwiftTests.swift */,
1085+
AC85A748216D24F4005241AE /* NotificationExtensionTests.swift */,
10861086
AC9A49AC20F419AA0007A5A2 /* TestFileTests.swift */,
10871087
);
10881088
path = "notification-extension-tests";
@@ -1875,7 +1875,7 @@
18751875
isa = PBXSourcesBuildPhase;
18761876
buildActionMask = 2147483647;
18771877
files = (
1878-
AC85A749216D24F4005241AE /* NotificationExtensionSwiftTests.swift in Sources */,
1878+
AC85A749216D24F4005241AE /* NotificationExtensionTests.swift in Sources */,
18791879
AC9A49AE20F41B520007A5A2 /* TestFileTests.swift in Sources */,
18801880
);
18811881
runOnlyForDeploymentPostprocessing = 0;

tests/notification-extension-tests/NotificationExtensionSwiftTests.swift renamed to tests/notification-extension-tests/NotificationExtensionTests.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import UniformTypeIdentifiers
99

1010
@testable import IterableAppExtensions
1111

12-
class NotificationExtensionSwiftTests: XCTestCase {
12+
class NotificationExtensionTests: XCTestCase {
1313
private var appExtension: ITBNotificationServiceExtension!
1414
private let delay = 0.05
1515
private let timeout = 15.0
@@ -385,7 +385,6 @@ class NotificationExtensionSwiftTests: XCTestCase {
385385
"type": "openUrl",
386386
"data": "http://maps.apple.com/?ll=37.7828,-122.3984"
387387
],
388-
"systemImageName-invalid": "hand.thumbsup",
389388
]],
390389
],
391390
]
@@ -427,7 +426,10 @@ class NotificationExtensionSwiftTests: XCTestCase {
427426
"type": "openUrl",
428427
"data": "http://maps.apple.com/?ll=37.7828,-122.3984"
429428
],
430-
"systemImageName": "hand.thumbsup",
429+
"actionIcon": [
430+
"iconType": "systemImage",
431+
"imageName": "hand.thumbsup",
432+
],
431433
]],
432434
],
433435
]
@@ -468,7 +470,10 @@ class NotificationExtensionSwiftTests: XCTestCase {
468470
"type": "openUrl",
469471
"data": "http://maps.apple.com/?ll=37.7828,-122.3984"
470472
],
471-
"templateImageName": "custom.thumbsup",
473+
"actionIcon": [
474+
"iconType": "templateImage",
475+
"imageName": "custom.thumbsup",
476+
],
472477
]],
473478
],
474479
]

0 commit comments

Comments
 (0)