调整代码

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")
@ -34,8 +33,6 @@ class NotificationService: UNNotificationServiceExtension {
// 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
} }
} }
} }
@ -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")
@ -157,22 +154,28 @@ class NotificationService: UNNotificationServiceExtension {
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,28 +222,30 @@ 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)
return
}
if let bestAttemptContent = bestAttemptContent {
let userInfo = bestAttemptContent.userInfo let userInfo = bestAttemptContent.userInfo
// //
@ -248,16 +253,11 @@ class NotificationService: UNNotificationServiceExtension {
// //
archive(userInfo) archive(userInfo)
// //
setIcon(userInfo, content: self.bestAttemptContent!) { setIcon(content: bestAttemptContent) { result in
// //
self.setImage(userInfo, content: self.bestAttemptContent!) { self.setImage(content: result) { result in
contentHandler(self.bestAttemptContent!) contentHandler(result)
} }
}
}
else{
contentHandler(bestAttemptContent!)
} }
} }
} }