Skip to content

Commit 57e8f47

Browse files
committed
NSE: readjust end states + refactor getCategoryId
1 parent 7082245 commit 57e8f47

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

notification-extension/ITBNotificationServiceExtension.swift

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import UserNotifications
1212
self.contentHandler = contentHandler
1313
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
1414

15-
setCategoryId(from: request.content)
15+
getCategoryId(from: request.content)
1616
retrieveAttachment(from: request.content)
1717

1818
checkPushCreationCompletion()
@@ -76,54 +76,42 @@ import UserNotifications
7676
attachmentDownloadTask = nil
7777
}
7878

79-
private func setCategoryId(from content: UNNotificationContent) {
80-
// IMPORTANT: need to add this to the documentation
81-
bestAttemptContent?.categoryIdentifier = getCategoryId(from: content)
82-
83-
getCategoryIdFinished = true
84-
}
85-
86-
private func getCategoryId(from content: UNNotificationContent) -> String {
79+
private func getCategoryId(from content: UNNotificationContent) {
8780
guard content.categoryIdentifier.count == 0 else {
88-
return content.categoryIdentifier
89-
}
90-
91-
guard let itblDictionary = content.userInfo[JsonKey.Payload.metadata] as? [AnyHashable: Any],
92-
let messageId = itblDictionary[JsonKey.Payload.messageId] as? String else {
93-
return ""
94-
}
95-
96-
var actionButtons: [[AnyHashable: Any]] = []
97-
98-
if let actionButtonsFromITBLPayload = itblDictionary[JsonKey.Payload.actionButtons] as? [[AnyHashable: Any]] {
99-
actionButtons = actionButtonsFromITBLPayload
100-
} else {
101-
#if DEBUG
102-
if let actionButtonsFromUserInfo = content.userInfo[JsonKey.Payload.actionButtons] as? [[AnyHashable: Any]] {
103-
actionButtons = actionButtonsFromUserInfo
104-
}
105-
#endif
81+
setCategoryId(id: content.categoryIdentifier)
82+
return
10683
}
10784

108-
var notificationActions = [UNNotificationAction]()
109-
110-
for actionButton in actionButtons {
111-
if let notificationAction = createNotificationActionButton(buttonDictionary: actionButton) {
112-
notificationActions.append(notificationAction)
113-
}
85+
guard let itblPayload = content.userInfo[JsonKey.Payload.metadata] as? [AnyHashable: Any],
86+
let messageId = itblPayload[JsonKey.Payload.messageId] as? String else {
87+
setCategoryId(id: "")
88+
return
11489
}
11590

116-
messageCategory = UNNotificationCategory(identifier: messageId, actions: notificationActions, intentIdentifiers: [], options: [])
91+
messageCategory = UNNotificationCategory(identifier: messageId,
92+
actions: getNotificationActions(payload: itblPayload, content: content),
93+
intentIdentifiers: [],
94+
options: [])
11795

11896
if let messageCategory = messageCategory {
119-
UNUserNotificationCenter.current().getNotificationCategories { categories in
97+
UNUserNotificationCenter.current().getNotificationCategories { [weak self] categories in
12098
var newCategories = categories
12199
newCategories.insert(messageCategory)
122100
UNUserNotificationCenter.current().setNotificationCategories(newCategories)
101+
self?.setCategoryId(id: messageId)
123102
}
103+
} else {
104+
setCategoryId(id: messageId)
124105
}
106+
}
107+
108+
private func setCategoryId(id: String) {
109+
// IMPORTANT: need to add this to the documentation
110+
bestAttemptContent?.categoryIdentifier = id
125111

126-
return messageId
112+
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
113+
self.getCategoryIdFinished = true
114+
}
127115
}
128116

129117
private func createNotificationActionButton(buttonDictionary: [AnyHashable: Any]) -> UNNotificationAction? {
@@ -182,12 +170,20 @@ import UserNotifications
182170
return IterableButtonTypeDefault
183171
}
184172

185-
private static func getAttachmentIdSuffix(response: URLResponse, responseUrl: URL) -> String {
186-
if let suggestedFilename = response.suggestedFilename {
187-
return suggestedFilename
173+
private func getNotificationActions(payload: [AnyHashable: Any], content: UNNotificationContent) -> [UNNotificationAction] {
174+
var actionButtons: [[AnyHashable: Any]] = []
175+
176+
if let actionButtonsFromItblPayload = payload[JsonKey.Payload.actionButtons] as? [[AnyHashable: Any]] {
177+
actionButtons = actionButtonsFromItblPayload
178+
} else {
179+
#if DEBUG
180+
if let actionButtonsFromUserInfo = content.userInfo[JsonKey.Payload.actionButtons] as? [[AnyHashable: Any]] {
181+
actionButtons = actionButtonsFromUserInfo
182+
}
183+
#endif
188184
}
189185

190-
return responseUrl.lastPathComponent
186+
return actionButtons.compactMap { createNotificationActionButton(buttonDictionary: $0) }
191187
}
192188

193189
private func checkPushCreationCompletion() {
@@ -204,6 +200,14 @@ import UserNotifications
204200
}
205201
}
206202

203+
private static func getAttachmentIdSuffix(response: URLResponse, responseUrl: URL) -> String {
204+
if let suggestedFilename = response.suggestedFilename {
205+
return suggestedFilename
206+
}
207+
208+
return responseUrl.lastPathComponent
209+
}
210+
207211
private var getCategoryIdFinished: Bool = false {
208212
didSet {
209213
checkPushCreationCompletion()

0 commit comments

Comments
 (0)