调整代码

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