🐛 Fix the NSE again...
This commit is contained in:
@@ -9,6 +9,7 @@ import UserNotifications
|
|||||||
import Intents
|
import Intents
|
||||||
import Kingfisher
|
import Kingfisher
|
||||||
import UniformTypeIdentifiers
|
import UniformTypeIdentifiers
|
||||||
|
import KingfisherWebP
|
||||||
|
|
||||||
enum ParseNotificationPayloadError: Error {
|
enum ParseNotificationPayloadError: Error {
|
||||||
case missingMetadata(String)
|
case missingMetadata(String)
|
||||||
@@ -24,6 +25,11 @@ class NotificationService: UNNotificationServiceExtension {
|
|||||||
_ request: UNNotificationRequest,
|
_ request: UNNotificationRequest,
|
||||||
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
|
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
|
||||||
) {
|
) {
|
||||||
|
KingfisherManager.shared.defaultOptions += [
|
||||||
|
.processor(WebPProcessor.default),
|
||||||
|
.cacheSerializer(WebPSerializer.default)
|
||||||
|
]
|
||||||
|
|
||||||
self.contentHandler = contentHandler
|
self.contentHandler = contentHandler
|
||||||
guard let bestAttemptContent = request.content.mutableCopy() as? UNMutableNotificationContent else {
|
guard let bestAttemptContent = request.content.mutableCopy() as? UNMutableNotificationContent else {
|
||||||
contentHandler(request.content)
|
contentHandler(request.content)
|
||||||
@@ -64,40 +70,12 @@ class NotificationService: UNNotificationServiceExtension {
|
|||||||
|
|
||||||
let handle = INPersonHandle(value: "\(metaCopy["user_id"] ?? "")", type: .unknown)
|
let handle = INPersonHandle(value: "\(metaCopy["user_id"] ?? "")", type: .unknown)
|
||||||
|
|
||||||
if let pfpUrl = pfpUrl, let url = URL(string: pfpUrl) {
|
let completeNotificationProcessing: (Data?) -> Void = { imageData in
|
||||||
let targetSize = 512
|
|
||||||
let scaleProcessor = ResizingImageProcessor(referenceSize: CGSize(width: targetSize, height: targetSize), mode: .aspectFit)
|
|
||||||
|
|
||||||
KingfisherManager.shared.retrieveImage(with: url, options: [.processor(scaleProcessor)], completionHandler: { result in
|
|
||||||
var image: Data?
|
|
||||||
switch result {
|
|
||||||
case .success(let value):
|
|
||||||
image = value.image.pngData()
|
|
||||||
case .failure(let error):
|
|
||||||
print("Unable to get pfp url: \(error)")
|
|
||||||
}
|
|
||||||
|
|
||||||
let sender = INPerson(
|
|
||||||
personHandle: handle,
|
|
||||||
nameComponents: PersonNameComponents(nickname: "\(metaCopy["sender_name"] ?? "")"),
|
|
||||||
displayName: content.title,
|
|
||||||
image: image == nil ? nil : INImage(imageData: image!),
|
|
||||||
contactIdentifier: nil,
|
|
||||||
customIdentifier: nil
|
|
||||||
)
|
|
||||||
|
|
||||||
let intent = self.createMessageIntent(with: sender, meta: metaCopy, body: content.body)
|
|
||||||
self.donateInteraction(for: intent)
|
|
||||||
|
|
||||||
content.categoryIdentifier = "CHAT_MESSAGE"
|
|
||||||
self.contentHandler?(content)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
let sender = INPerson(
|
let sender = INPerson(
|
||||||
personHandle: handle,
|
personHandle: handle,
|
||||||
nameComponents: PersonNameComponents(nickname: "\(metaCopy["sender_name"] ?? "")"),
|
nameComponents: PersonNameComponents(nickname: "\(metaCopy["sender_name"] ?? "")"),
|
||||||
displayName: content.title,
|
displayName: content.title,
|
||||||
image: nil,
|
image: imageData == nil ? nil : INImage(imageData: imageData!),
|
||||||
contactIdentifier: nil,
|
contactIdentifier: nil,
|
||||||
customIdentifier: nil
|
customIdentifier: nil
|
||||||
)
|
)
|
||||||
@@ -105,8 +83,37 @@ class NotificationService: UNNotificationServiceExtension {
|
|||||||
let intent = self.createMessageIntent(with: sender, meta: metaCopy, body: content.body)
|
let intent = self.createMessageIntent(with: sender, meta: metaCopy, body: content.body)
|
||||||
self.donateInteraction(for: intent)
|
self.donateInteraction(for: intent)
|
||||||
|
|
||||||
content.categoryIdentifier = "CHAT_MESSAGE"
|
if let updatedContent = try? request.content.updating(from: intent) {
|
||||||
self.contentHandler?(content)
|
if let mutableContent = updatedContent.mutableCopy() as? UNMutableNotificationContent {
|
||||||
|
mutableContent.categoryIdentifier = "CHAT_MESSAGE"
|
||||||
|
self.contentHandler?(mutableContent)
|
||||||
|
} else {
|
||||||
|
self.contentHandler?(updatedContent)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
content.categoryIdentifier = "CHAT_MESSAGE"
|
||||||
|
self.contentHandler?(content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let pfpUrl = pfpUrl, let url = URL(string: pfpUrl) {
|
||||||
|
let targetSize = 512
|
||||||
|
let scaleProcessor = ResizingImageProcessor(referenceSize: CGSize(width: targetSize, height: targetSize), mode: .aspectFit)
|
||||||
|
|
||||||
|
KingfisherManager.shared.retrieveImage(with: url, options: [
|
||||||
|
.processor(scaleProcessor)
|
||||||
|
], completionHandler: { result in
|
||||||
|
var image: Data?
|
||||||
|
switch result {
|
||||||
|
case .success(let value):
|
||||||
|
image = value.image.pngData()
|
||||||
|
case .failure(let error):
|
||||||
|
print("Unable to get pfp url: \(error)")
|
||||||
|
}
|
||||||
|
completeNotificationProcessing(image)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
completeNotificationProcessing(nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user