@@ -12,7 +12,7 @@ import UserNotifications
12
12
self . contentHandler = contentHandler
13
13
bestAttemptContent = ( request. content. mutableCopy ( ) as? UNMutableNotificationContent )
14
14
15
- setCategoryId ( from: request. content)
15
+ getCategoryId ( from: request. content)
16
16
retrieveAttachment ( from: request. content)
17
17
18
18
checkPushCreationCompletion ( )
@@ -76,54 +76,42 @@ import UserNotifications
76
76
attachmentDownloadTask = nil
77
77
}
78
78
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 ) {
87
80
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
106
83
}
107
84
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
114
89
}
115
90
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: [ ] )
117
95
118
96
if let messageCategory = messageCategory {
119
- UNUserNotificationCenter . current ( ) . getNotificationCategories { categories in
97
+ UNUserNotificationCenter . current ( ) . getNotificationCategories { [ weak self ] categories in
120
98
var newCategories = categories
121
99
newCategories. insert ( messageCategory)
122
100
UNUserNotificationCenter . current ( ) . setNotificationCategories ( newCategories)
101
+ self ? . setCategoryId ( id: messageId)
123
102
}
103
+ } else {
104
+ setCategoryId ( id: messageId)
124
105
}
106
+ }
107
+
108
+ private func setCategoryId( id: String ) {
109
+ // IMPORTANT: need to add this to the documentation
110
+ bestAttemptContent? . categoryIdentifier = id
125
111
126
- return messageId
112
+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 1 ) {
113
+ self . getCategoryIdFinished = true
114
+ }
127
115
}
128
116
129
117
private func createNotificationActionButton( buttonDictionary: [ AnyHashable : Any ] ) -> UNNotificationAction ? {
@@ -182,12 +170,20 @@ import UserNotifications
182
170
return IterableButtonTypeDefault
183
171
}
184
172
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
188
184
}
189
185
190
- return responseUrl . lastPathComponent
186
+ return actionButtons . compactMap { createNotificationActionButton ( buttonDictionary : $0 ) }
191
187
}
192
188
193
189
private func checkPushCreationCompletion( ) {
@@ -204,6 +200,14 @@ import UserNotifications
204
200
}
205
201
}
206
202
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
+
207
211
private var getCategoryIdFinished : Bool = false {
208
212
didSet {
209
213
checkPushCreationCompletion ( )
0 commit comments