@@ -8,7 +8,8 @@ import UserNotifications
8
8
var contentHandler : ( ( UNNotificationContent ) -> Void ) ?
9
9
var bestAttemptContent : UNMutableNotificationContent ?
10
10
11
- @objc override open func didReceive( _ request: UNNotificationRequest , withContentHandler contentHandler: @escaping ( UNNotificationContent ) -> Void ) {
11
+ @objc override open func didReceive( _ request: UNNotificationRequest ,
12
+ withContentHandler contentHandler: @escaping ( UNNotificationContent ) -> Void ) {
12
13
self . contentHandler = contentHandler
13
14
bestAttemptContent = ( request. content. mutableCopy ( ) as? UNMutableNotificationContent )
14
15
@@ -28,8 +29,8 @@ import UserNotifications
28
29
// MARK: - Private
29
30
30
31
private func retrieveAttachment( from content: UNNotificationContent ) {
31
- guard let itblDictionary = content. userInfo [ JsonKey . Payload. metadata] as? [ AnyHashable : Any ] ,
32
- let attachmentUrlString = itblDictionary [ JsonKey . Payload. attachmentUrl] as? String ,
32
+ guard let metadata = content. userInfo [ JsonKey . Payload. metadata] as? [ AnyHashable : Any ] ,
33
+ let attachmentUrlString = metadata [ JsonKey . Payload. attachmentUrl] as? String ,
33
34
let url = URL ( string: attachmentUrlString) else {
34
35
attachmentRetrievalFinished = true
35
36
return
@@ -48,7 +49,8 @@ import UserNotifications
48
49
return
49
50
}
50
51
51
- let attachmentId = UUID ( ) . uuidString + ITBNotificationServiceExtension. getAttachmentIdSuffix ( response: response, responseUrl: responseUrl)
52
+ let attachmentId = UUID ( ) . uuidString + ITBNotificationServiceExtension. getAttachmentIdSuffix ( response: response,
53
+ responseUrl: responseUrl)
52
54
let tempFileUrl = FileManager . default. temporaryDirectory. appendingPathComponent ( attachmentId)
53
55
54
56
var attachment : UNNotificationAttachment ?
@@ -82,14 +84,15 @@ import UserNotifications
82
84
return
83
85
}
84
86
85
- guard let itblPayload = content. userInfo [ JsonKey . Payload. metadata] as? [ AnyHashable : Any ] ,
86
- let messageId = itblPayload [ JsonKey . Payload. messageId] as? String else {
87
+ guard let metadata = content. userInfo [ JsonKey . Payload. metadata] as? [ AnyHashable : Any ] ,
88
+ let messageId = metadata [ JsonKey . Payload. messageId] as? String else {
87
89
setCategoryId ( id: " " )
88
90
return
89
91
}
90
92
91
93
messageCategory = UNNotificationCategory ( identifier: messageId,
92
- actions: getNotificationActions ( payload: itblPayload, content: content) ,
94
+ actions: getNotificationActions ( metadata: metadata,
95
+ content: content) ,
93
96
intentIdentifiers: [ ] ,
94
97
options: [ ] )
95
98
@@ -116,54 +119,50 @@ import UserNotifications
116
119
}
117
120
}
118
121
119
- private func createNotificationActionButton( buttonDictionary: [ AnyHashable : Any ] ) -> UNNotificationAction ? {
120
- guard let identifier = buttonDictionary [ JsonKey . ActionButton. identifier] as? String else { return nil }
121
- guard let title = buttonDictionary [ JsonKey . ActionButton. title] as? String else { return nil }
122
-
123
- let buttonType = getButtonType ( buttonDictionary: buttonDictionary)
124
-
125
- var openApp = true
122
+ private func getNotificationActions( metadata: [ AnyHashable : Any ] , content: UNNotificationContent ) -> [ UNNotificationAction ] {
123
+ var actionButtons : [ [ AnyHashable : Any ] ] = [ ]
126
124
127
- if let openAppFromDict = buttonDictionary [ JsonKey . ActionButton. openApp] as? NSNumber {
128
- openApp = openAppFromDict. boolValue
125
+ if let actionButtonsFromMetadata = metadata [ JsonKey . Payload. actionButtons] as? [ [ AnyHashable : Any ] ] {
126
+ actionButtons = actionButtonsFromMetadata
127
+ } else {
128
+ #if DEBUG
129
+ if let actionButtonsFromUserInfo = content. userInfo [ JsonKey . Payload. actionButtons] as? [ [ AnyHashable : Any ] ] {
130
+ actionButtons = actionButtonsFromUserInfo
131
+ }
132
+ #endif
129
133
}
130
134
131
- var requiresUnlock = false
132
-
133
- if let requiresUnlockFromDict = buttonDictionary [ JsonKey . ActionButton. requiresUnlock] as? NSNumber {
134
- requiresUnlock = requiresUnlockFromDict. boolValue
135
- }
135
+ return actionButtons. compactMap { createNotificationActionButton ( info: $0) }
136
+ }
137
+
138
+ private func createNotificationActionButton( info: [ AnyHashable : Any ] ) -> UNNotificationAction ? {
139
+ guard let identifier = info [ JsonKey . ActionButton. identifier] as? String else { return nil }
140
+ guard let title = info [ JsonKey . ActionButton. title] as? String else { return nil }
136
141
137
- var actionOptions : UNNotificationActionOptions = [ ]
142
+ let buttonType = getButtonType ( info: info)
143
+ let openApp = getBoolValue ( info [ JsonKey . ActionButton. openApp] ) ?? true
144
+ let requiresUnlock = getBoolValue ( info [ JsonKey . ActionButton. requiresUnlock] ) ?? false
138
145
139
- if buttonType == IterableButtonTypeDestructive {
140
- actionOptions . insert ( . destructive )
141
- }
146
+ let options = getActionButtonOptions ( buttonType : buttonType ,
147
+ openApp : openApp ,
148
+ requiresUnlock : requiresUnlock )
142
149
143
- if openApp {
144
- actionOptions . insert ( . foreground )
150
+ guard buttonType == IterableButtonTypeTextInput else {
151
+ return UNNotificationAction ( identifier : identifier , title : title , options : options )
145
152
}
146
153
147
- if requiresUnlock || openApp {
148
- actionOptions. insert ( . authenticationRequired)
149
- }
154
+ let inputTitle = info [ JsonKey . ActionButton. inputTitle] as? String ?? " "
155
+ let inputPlaceholder = info [ JsonKey . ActionButton. inputPlaceholder] as? String ?? " "
150
156
151
- if buttonType == IterableButtonTypeTextInput {
152
- let inputTitle = buttonDictionary [ JsonKey . ActionButton. inputTitle] as? String ?? " "
153
- let inputPlaceholder = buttonDictionary [ JsonKey . ActionButton. inputPlaceholder] as? String ?? " "
154
-
155
- return UNTextInputNotificationAction ( identifier: identifier,
156
- title: title,
157
- options: actionOptions,
158
- textInputButtonTitle: inputTitle,
159
- textInputPlaceholder: inputPlaceholder)
160
- } else {
161
- return UNNotificationAction ( identifier: identifier, title: title, options: actionOptions)
162
- }
157
+ return UNTextInputNotificationAction ( identifier: identifier,
158
+ title: title,
159
+ options: options,
160
+ textInputButtonTitle: inputTitle,
161
+ textInputPlaceholder: inputPlaceholder)
163
162
}
164
163
165
- private func getButtonType( buttonDictionary : [ AnyHashable : Any ] ) -> String {
166
- if let buttonType = buttonDictionary [ JsonKey . ActionButton. buttonType] as? String {
164
+ private func getButtonType( info : [ AnyHashable : Any ] ) -> String {
165
+ if let buttonType = info [ JsonKey . ActionButton. buttonType] as? String {
167
166
if buttonType == IterableButtonTypeTextInput || buttonType == IterableButtonTypeDestructive {
168
167
return buttonType
169
168
}
@@ -172,20 +171,26 @@ import UserNotifications
172
171
return IterableButtonTypeDefault
173
172
}
174
173
175
- private func getNotificationActions( payload: [ AnyHashable : Any ] , content: UNNotificationContent ) -> [ UNNotificationAction ] {
176
- var actionButtons : [ [ AnyHashable : Any ] ] = [ ]
174
+ private func getBoolValue( _ value: Any ? ) -> Bool ? {
175
+ return ( value as? NSNumber ) ? . boolValue
176
+ }
177
+
178
+ private func getActionButtonOptions( buttonType: String , openApp: Bool , requiresUnlock: Bool ) -> UNNotificationActionOptions {
179
+ var options : UNNotificationActionOptions = [ ]
177
180
178
- if let actionButtonsFromItblPayload = payload [ JsonKey . Payload. actionButtons] as? [ [ AnyHashable : Any ] ] {
179
- actionButtons = actionButtonsFromItblPayload
180
- } else {
181
- #if DEBUG
182
- if let actionButtonsFromUserInfo = content. userInfo [ JsonKey . Payload. actionButtons] as? [ [ AnyHashable : Any ] ] {
183
- actionButtons = actionButtonsFromUserInfo
184
- }
185
- #endif
181
+ if buttonType == IterableButtonTypeDestructive {
182
+ options. insert ( . destructive)
183
+ }
184
+
185
+ if openApp {
186
+ options. insert ( . foreground)
187
+ }
188
+
189
+ if requiresUnlock || openApp {
190
+ options. insert ( . authenticationRequired)
186
191
}
187
192
188
- return actionButtons . compactMap { createNotificationActionButton ( buttonDictionary : $0 ) }
193
+ return options
189
194
}
190
195
191
196
private func checkPushCreationCompletion( ) {
0 commit comments