调整代码

This commit is contained in:
Fin 2021-10-11 12:01:26 +08:00
parent edef0e8d10
commit 7be222b370

View File

@ -15,7 +15,6 @@ import Intents
class NotificationService: UNNotificationServiceExtension { class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)? var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
lazy var realm:Realm? = { lazy var realm:Realm? = {
let groupUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark") let groupUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark")
@ -30,12 +29,10 @@ class NotificationService: UNNotificationServiceExtension {
// Realm will automatically detect new properties and removed properties // Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically // And will update the schema on disk automatically
} }
}) })
// Tell Realm to use this new configuration object for the default Realm // Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config Realm.Configuration.defaultConfiguration = config
return try? Realm() return try? Realm()
}() }()
@ -44,14 +41,14 @@ class NotificationService: UNNotificationServiceExtension {
/// - Parameters: /// - Parameters:
/// - userInfo: /// - userInfo:
/// - bestAttemptContentBody: body`` `` /// - bestAttemptContentBody: body`` ``
fileprivate func autoCopy(_ userInfo: [AnyHashable : Any], defaultCopy bestAttemptContentBody: String) { fileprivate func autoCopy(_ userInfo: [AnyHashable : Any], defaultCopy: String) {
if userInfo["autocopy"] as? String == "1" if userInfo["autocopy"] as? String == "1"
|| userInfo["automaticallycopy"] as? String == "1"{ || userInfo["automaticallycopy"] as? String == "1"{
if let copy = userInfo["copy"] as? String { if let copy = userInfo["copy"] as? String {
UIPasteboard.general.string = copy UIPasteboard.general.string = copy
} }
else{ else{
UIPasteboard.general.string = bestAttemptContentBody UIPasteboard.general.string = defaultCopy
} }
} }
} }
@ -108,7 +105,7 @@ class NotificationService: UNNotificationServiceExtension {
let cacheFileUrl = cache.cachePath(forKey: imageResource.cacheKey) let cacheFileUrl = cache.cachePath(forKey: imageResource.cacheKey)
complection(cacheFileUrl) complection(cacheFileUrl)
} }
// //
if cache.diskStorage.isCached(forKey: imageResource.cacheKey) { if cache.diskStorage.isCached(forKey: imageResource.cacheKey) {
downloadFinished() downloadFinished()
@ -128,21 +125,21 @@ class NotificationService: UNNotificationServiceExtension {
} }
} }
/// ///
/// - Parameters: /// - Parameters:
/// - userInfo:
/// - bestAttemptContent: content /// - bestAttemptContent: content
/// - complection: /// - complection:
fileprivate func setImage(_ userInfo: [AnyHashable : Any], content bestAttemptContent: UNMutableNotificationContent, complection: @escaping () -> () ) { fileprivate func setImage(content bestAttemptContent: UNMutableNotificationContent,
complection: @escaping (_ content:UNMutableNotificationContent) -> () ) {
let userInfo = bestAttemptContent.userInfo
guard let imageUrl = userInfo["image"] as? String else { guard let imageUrl = userInfo["image"] as? String else {
complection() complection(bestAttemptContent)
return return
} }
func finished(_ imageFileUrl: String?){ func finished(_ imageFileUrl: String?){
guard let imageFileUrl = imageFileUrl else { guard let imageFileUrl = imageFileUrl else {
complection() complection(bestAttemptContent)
return return
} }
let copyDestUrl = URL(fileURLWithPath: imageFileUrl).appendingPathExtension(".tmp") let copyDestUrl = URL(fileURLWithPath: imageFileUrl).appendingPathExtension(".tmp")
@ -152,27 +149,33 @@ class NotificationService: UNNotificationServiceExtension {
to: copyDestUrl) to: copyDestUrl)
if let attachment = try? UNNotificationAttachment( if let attachment = try? UNNotificationAttachment(
identifier: "image", identifier: "image",
url: copyDestUrl, url: copyDestUrl,
options: [UNNotificationAttachmentOptionsTypeHintKey : kUTTypePNG]){ options: [UNNotificationAttachmentOptionsTypeHintKey : kUTTypePNG]){
bestAttemptContent.attachments = [ attachment ] bestAttemptContent.attachments = [ attachment ]
} }
complection() complection(bestAttemptContent)
} }
downloadImage(imageUrl, complection: finished) downloadImage(imageUrl, complection: finished)
} }
fileprivate func setIcon(_ userInfo: [AnyHashable : Any], content bestAttemptContent: UNMutableNotificationContent, complection: @escaping () -> () ) { /// icon
/// - Parameters:
/// - bestAttemptContent: content
/// - complection:
fileprivate func setIcon(content bestAttemptContent: UNMutableNotificationContent,
complection: @escaping (_ content:UNMutableNotificationContent) -> () ) {
if #available(iOSApplicationExtension 15.0, *) { if #available(iOSApplicationExtension 15.0, *) {
let userInfo = bestAttemptContent.userInfo
guard let imageUrl = userInfo["icon"] as? String else { guard let imageUrl = userInfo["icon"] as? String else {
complection() complection(bestAttemptContent)
return return
} }
func finished(_ imageFileUrl: String?){ func finished(_ imageFileUrl: String?){
guard let imageFileUrl = imageFileUrl else { guard let imageFileUrl = imageFileUrl else {
complection() complection(bestAttemptContent)
return return
} }
var personNameComponents = PersonNameComponents() var personNameComponents = PersonNameComponents()
@ -205,7 +208,7 @@ class NotificationService: UNNotificationServiceExtension {
outgoingMessageType: .outgoingMessageText, outgoingMessageType: .outgoingMessageText,
content: bestAttemptContent.body, content: bestAttemptContent.body,
speakableGroupName: INSpeakableString(spokenPhrase: personNameComponents.nickname ?? ""), speakableGroupName: INSpeakableString(spokenPhrase: personNameComponents.nickname ?? ""),
conversationIdentifier: "sampleConversationIdentifier", conversationIdentifier: bestAttemptContent.threadIdentifier,
serviceName: nil, serviceName: nil,
sender: senderPerson, sender: senderPerson,
attachments: nil attachments: nil
@ -219,45 +222,42 @@ class NotificationService: UNNotificationServiceExtension {
interaction.donate(completion: nil) interaction.donate(completion: nil)
do { do {
let content = try self.bestAttemptContent!.updating(from: intent) as! UNMutableNotificationContent let content = try bestAttemptContent.updating(from: intent) as! UNMutableNotificationContent
self.bestAttemptContent = content complection(content)
} catch { } catch {
// Handle error // Handle error
} }
complection() complection(bestAttemptContent)
} }
downloadImage(imageUrl, complection: finished) downloadImage(imageUrl, complection: finished)
} }
else{ else{
complection() complection(bestAttemptContent)
} }
} }
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) guard let bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
contentHandler(request.content)
if let bestAttemptContent = bestAttemptContent { return
let userInfo = bestAttemptContent.userInfo
//
autoCopy(userInfo, defaultCopy: bestAttemptContent.body)
//
archive(userInfo)
//
setIcon(userInfo, content: self.bestAttemptContent!) {
//
self.setImage(userInfo, content: self.bestAttemptContent!) {
contentHandler(self.bestAttemptContent!)
}
}
} }
else{
contentHandler(bestAttemptContent!) let userInfo = bestAttemptContent.userInfo
//
autoCopy(userInfo, defaultCopy: bestAttemptContent.body)
//
archive(userInfo)
//
setIcon(content: bestAttemptContent) { result in
//
self.setImage(content: result) { result in
contentHandler(result)
}
} }
} }
} }