mirror of
https://github.com/Finb/Bark.git
synced 2025-12-08 21:36:01 +00:00
调整代码
This commit is contained in:
parent
edef0e8d10
commit
7be222b370
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user