支持图片推送 image 参数。

This commit is contained in:
Fin 2021-09-07 17:31:33 +08:00
parent b77d08a9dc
commit 7e82fe8f7b
3 changed files with 135 additions and 39 deletions

View File

@ -9,6 +9,8 @@
import UIKit
import UserNotifications
import RealmSwift
import Kingfisher
import MobileCoreServices
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
@ -36,6 +38,122 @@ class NotificationService: UNNotificationServiceExtension {
return try? Realm()
}()
///
/// - Parameters:
/// - userInfo:
/// - bestAttemptContentBody: body`` ``
fileprivate func autoCopy(_ userInfo: [AnyHashable : Any], defaultCopy bestAttemptContentBody: 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
}
}
}
///
/// - Parameter userInfo:
/// `isarchive` `isarchive`
/// ``
fileprivate func archive(_ userInfo: [AnyHashable : Any]) {
var isArchive:Bool?
if let archive = userInfo["isarchive"] as? String{
isArchive = archive == "1" ? true : false
}
if isArchive == nil {
isArchive = ArchiveSettingManager.shared.isArchive
}
let alert = (userInfo["aps"] as? [String:Any])?["alert"] as? [String:Any]
let title = alert?["title"] as? String
let body = alert?["body"] as? String
let url = userInfo["url"] as? String
let group = userInfo["group"] as? String
if (isArchive == true){
try? realm?.write{
let message = Message()
message.title = title
message.body = body
message.url = url
message.group = group
message.createDate = Date()
realm?.add(message)
}
}
}
///
/// - Parameters:
/// - userInfo:
/// - bestAttemptContent: content
/// - complection:
fileprivate func downloadImage(_ userInfo: [AnyHashable : Any], content bestAttemptContent: UNMutableNotificationContent, complection: @escaping () -> () ) {
guard let imageUrl = userInfo["image"] as? String,
let groupUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.bark"),
let cache = try? ImageCache(name: "shared",cacheDirectoryURL: groupUrl)
else {
complection()
return
}
//
if imageUrl.hasPrefix("http")
{
guard let imageResource = URL(string: imageUrl) else {
complection()
return
}
func finished(){
let cacheFileUrl = cache.cachePath(forKey: imageResource.cacheKey)
let copyDestUrl = URL(fileURLWithPath: cacheFileUrl).appendingPathExtension(".tmp")
// 使
try? FileManager.default.copyItem(
at: URL(fileURLWithPath: cacheFileUrl),
to: copyDestUrl)
if let attachment = try? UNNotificationAttachment(
identifier: "image",
url: copyDestUrl,
options: [UNNotificationAttachmentOptionsTypeHintKey : kUTTypePNG]){
bestAttemptContent.attachments = [ attachment ]
}
complection()
}
//
if cache.diskStorage.isCached(forKey: imageResource.cacheKey) {
finished()
return
}
//
Kingfisher.ImageDownloader.default.downloadImage(with: imageResource, options: nil) { result in
guard let result = try? result.get() else {
complection()
return
}
//
cache.storeToDisk(result.originalData, forKey: imageResource.cacheKey, expiration: StorageExpiration.never) { r in
finished()
}
}
}
//
else{
complection()
}
}
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
@ -43,46 +161,18 @@ class NotificationService: UNNotificationServiceExtension {
if let bestAttemptContent = bestAttemptContent {
let userInfo = bestAttemptContent.userInfo
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 = bestAttemptContent.body
}
}
var isArchive:Bool?
if let archive = userInfo["isarchive"] as? String{
isArchive = archive == "1" ? true : false
}
if isArchive == nil {
isArchive = ArchiveSettingManager.shared.isArchive
}
let alert = (userInfo["aps"] as? [String:Any])?["alert"] as? [String:Any]
let title = alert?["title"] as? String
let body = alert?["body"] as? String
let url = userInfo["url"] as? String
let group = userInfo["group"] as? String
if (isArchive == true){
try? realm?.write{
let message = Message()
message.title = title
message.body = body
message.url = url
message.group = group
message.createDate = Date()
realm?.add(message)
}
//
autoCopy(userInfo, defaultCopy: bestAttemptContent.body)
//
archive(userInfo)
//
downloadImage(userInfo, content: bestAttemptContent) {
contentHandler(bestAttemptContent)
}
contentHandler(bestAttemptContent)
}
else{
contentHandler(bestAttemptContent!)
}
}
}

View File

@ -23,6 +23,7 @@ def pods
pod 'NSObject+Rx'
pod 'MJRefresh'
pod 'Kingfisher'
end
target 'Bark' do
@ -32,4 +33,5 @@ end
target 'NotificationServiceExtension' do
pod 'IceCream'
pod 'Kingfisher'
end

View File

@ -6,6 +6,7 @@ PODS:
- FDFullscreenPopGesture (1.1)
- IceCream (2.0.0):
- RealmSwift
- Kingfisher (6.3.1)
- KVOController (1.2.0)
- Material (3.1.8):
- Material/Core (= 3.1.8)
@ -50,6 +51,7 @@ DEPENDENCIES:
- DeviceKit
- FDFullscreenPopGesture
- IceCream
- Kingfisher
- KVOController
- Material
- MJRefresh
@ -71,6 +73,7 @@ SPEC REPOS:
- Differentiator
- FDFullscreenPopGesture
- IceCream
- Kingfisher
- KVOController
- Material
- MJRefresh
@ -105,6 +108,7 @@ SPEC CHECKSUMS:
Differentiator: 886080237d9f87f322641dedbc5be257061b0602
FDFullscreenPopGesture: a8a620179e3d9c40e8e00256dcee1c1a27c6d0f0
IceCream: a7d6ffdef8e36ce1d089738e51d5e918c4862fd8
Kingfisher: 016c8b653a35add51dd34a3aba36b580041acc74
KVOController: d72ace34afea42468329623b3379ab3cd1d286b6
Material: a2a3f400a3b549d53ef89e56c58c4535b29db387
MJRefresh: 6afc955813966afb08305477dd7a0d9ad5e79a16
@ -123,6 +127,6 @@ SPEC CHECKSUMS:
SVProgressHUD: 1428aafac632c1f86f62aa4243ec12008d7a51d6
SwiftyJSON: 2f33a42c6fbc52764d96f13368585094bfd8aa5e
PODFILE CHECKSUM: 2599db391f0d98d2d2a59b52455c89c9e9037bce
PODFILE CHECKSUM: 04278e0ebf8ec1eff77dd7872c95ab61a385adca
COCOAPODS: 1.10.2