mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-11-22 16:31:00 +00:00
Handle image in push notifications
This commit is contained in:
parent
57b0b6982c
commit
da0dcbfe1c
1 changed files with 25 additions and 2 deletions
|
@ -3,6 +3,7 @@ import KeychainSwift
|
||||||
import Env
|
import Env
|
||||||
import CryptoKit
|
import CryptoKit
|
||||||
import Models
|
import Models
|
||||||
|
import UIKit
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
class NotificationService: UNNotificationServiceExtension {
|
class NotificationService: UNNotificationServiceExtension {
|
||||||
|
@ -53,7 +54,29 @@ class NotificationService: UNNotificationServiceExtension {
|
||||||
bestAttemptContent.body = notification.body.escape()
|
bestAttemptContent.body = notification.body.escape()
|
||||||
bestAttemptContent.userInfo["plaintext"] = plaintextData
|
bestAttemptContent.userInfo["plaintext"] = plaintextData
|
||||||
|
|
||||||
|
if let urlString = notification.icon,
|
||||||
|
let url = URL(string: urlString) {
|
||||||
|
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("notification-attachments")
|
||||||
|
try? FileManager.default.createDirectory(at: temporaryDirectoryURL, withIntermediateDirectories: true, attributes: nil)
|
||||||
|
let filename = url.lastPathComponent
|
||||||
|
let fileURL = temporaryDirectoryURL.appendingPathComponent(filename)
|
||||||
|
|
||||||
|
Task {
|
||||||
|
if let (data, _) = try? await URLSession.shared.data(for: .init(url: url)) {
|
||||||
|
if let image = UIImage(data: data) {
|
||||||
|
try? image.pngData()?.write(to: fileURL)
|
||||||
|
if let attachment = try? UNNotificationAttachment(identifier: filename, url: fileURL, options: nil) {
|
||||||
|
bestAttemptContent.attachments = [attachment]
|
||||||
|
}
|
||||||
|
}
|
||||||
contentHandler(bestAttemptContent)
|
contentHandler(bestAttemptContent)
|
||||||
|
} else {
|
||||||
|
contentHandler(bestAttemptContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
contentHandler(bestAttemptContent)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue