metatext-app-ios-iphone-ipad/Notification Service Extension/NotificationService.swift

111 lines
4.0 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
2021-01-30 02:14:22 +01:00
import Kingfisher
2020-08-31 01:33:11 +02:00
import Mastodon
2021-01-30 02:14:22 +01:00
import ServiceLayer
2020-09-05 04:31:43 +02:00
import UserNotifications
2020-11-09 07:22:20 +01:00
final class NotificationService: UNNotificationServiceExtension {
2021-01-30 02:14:22 +01:00
private let environment = AppEnvironment.live(
userNotificationCenter: .current(),
reduceMotion: { false })
override init() {
super.init()
}
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
guard let bestAttemptContent = bestAttemptContent else { return }
2021-02-04 06:45:09 +01:00
let parsingService = PushNotificationParsingService(environment: environment)
2021-02-04 06:24:00 +01:00
let decryptedJSON: Data
let identityId: Identity.Id
2021-02-04 06:45:09 +01:00
let pushNotification: PushNotification
do {
2021-02-04 06:45:09 +01:00
(decryptedJSON, identityId) = try parsingService.extractAndDecrypt(userInfo: request.content.userInfo)
pushNotification = try MastodonDecoder().decode(PushNotification.self, from: decryptedJSON)
} catch {
contentHandler(bestAttemptContent)
return
}
2021-02-04 09:30:37 +01:00
bestAttemptContent.userInfo[PushNotificationParsingService.pushNotificationUserInfoKey] = decryptedJSON
bestAttemptContent.title = pushNotification.title
bestAttemptContent.body = XMLUnescaper(string: pushNotification.body).unescape()
2021-02-04 06:24:00 +01:00
let appPreferences = AppPreferences(environment: environment)
2021-02-04 06:24:00 +01:00
if appPreferences.notificationSounds.contains(pushNotification.notificationType) {
bestAttemptContent.sound = .default
}
2021-01-30 02:14:22 +01:00
2021-02-04 06:24:00 +01:00
if appPreferences.notificationAccountName,
let accountName = try? AllIdentitiesService(environment: environment).identity(id: identityId)?.handle {
bestAttemptContent.subtitle = accountName
}
2021-01-30 02:14:22 +01:00
2021-02-04 06:24:00 +01:00
if appPreferences.notificationPictures {
2021-02-04 21:09:05 +01:00
Self.addImage(url: pushNotification.icon,
2021-02-04 06:24:00 +01:00
bestAttemptContent: bestAttemptContent,
contentHandler: contentHandler)
} else {
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
private extension NotificationService {
2021-02-04 21:09:05 +01:00
static func addImage(url: URL,
2021-02-04 06:24:00 +01:00
bestAttemptContent: UNMutableNotificationContent,
contentHandler: @escaping (UNNotificationContent) -> Void) {
2021-02-04 21:09:05 +01:00
let fileName = url.lastPathComponent
2021-02-04 06:24:00 +01:00
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(fileName)
2021-02-04 21:09:05 +01:00
KingfisherManager.shared.retrieveImage(with: url) {
2021-02-04 06:24:00 +01:00
switch $0 {
case let .success(result):
let format: ImageFormat
switch fileURL.pathExtension.lowercased() {
case "jpg", "jpeg":
format = .JPEG
case "gif":
format = .GIF
case "png":
format = .PNG
default:
format = .unknown
}
do {
try result.image.kf.data(format: format)?.write(to: fileURL)
bestAttemptContent.attachments =
[try UNNotificationAttachment(identifier: fileName, url: fileURL)]
contentHandler(bestAttemptContent)
} catch {
contentHandler(bestAttemptContent)
}
case .failure:
contentHandler(bestAttemptContent)
}
}
}
}