Bark/NotificationServiceExtension/NotificationService.swift

63 lines
2.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// NotificationService.swift
// NotificationServiceExtension
//
// Created by huangfeng on 2018/12/17.
// Copyright © 2018 Fin. All rights reserved.
//
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
/// Processor
var currentNotificationProcessor: NotificationContentProcessor? = nil
/// ContentHandler serviceExtensionTimeWillExpire Processor
var currentContentHandler: ((UNNotificationContent) -> Void)? = nil
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
Task {
guard var bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
contentHandler(request.content)
return
}
self.currentContentHandler = contentHandler
// processor
// ciphertext
// call Processor 退 ServiceExtension
let processors: [NotificationContentProcessorItem] = [
.ciphertext,
.level,
.badge,
.autoCopy,
.archive,
.setIcon,
.setImage,
.mute,
.call
]
// processor
for processor in processors.map({ $0.processor }) {
do {
self.currentNotificationProcessor = processor
bestAttemptContent = try await processor.process(identifier: request.identifier, content: bestAttemptContent)
} catch NotificationContentProcessorError.error(let content) {
contentHandler(content)
return
}
}
//
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
super.serviceExtensionTimeWillExpire()
if let handler = self.currentContentHandler {
self.currentNotificationProcessor?.serviceExtensionTimeWillExpire(contentHandler: handler)
}
}
}