mirror of
https://github.com/Finb/Bark.git
synced 2025-12-08 21:36:01 +00:00
支持图片推送 image 参数。
This commit is contained in:
parent
b77d08a9dc
commit
7e82fe8f7b
@ -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!)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
2
Podfile
2
Podfile
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user