import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
bestAttemptContent.title = "\(bestAttemptContent.title)"
let audio:String = bestAttemptContent.userInfo["audio"] as? String ?? ""
if audio.count > 0 {
self.loadSoundsWithUserInfo(bestAttemptContent.userInfo)
}else{
self.dealShowImgWithUserInfo(bestAttemptContent.userInfo)
}
}
}
func loadSoundsWithUserInfo(_ userInfo:[AnyHashable : Any]) {
if let urlStr = userInfo["audio"] as? String, let url = URL(string: urlStr) {
let task = URLSession.shared.downloadTask(with: url) { (result, response, error) in
if (error == nil) {
if let result = result {
let groupUrl:URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.dsxindianshang.business")!
let groupPath:String = groupUrl.path
let filePath = groupPath + "/Library/Sounds"
print("文件路径=" + filePath)
if !FileManager.default.fileExists(atPath: filePath) {
do {
print("无此文件夹-创建文件夹")
try FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: true, attributes: nil)
} catch {
self.contentHandler!(self.bestAttemptContent!)
}
}
let fileName = "/wkw.wav"
print("文件名=" + fileName)
let savePath = filePath + fileName
print("文件存储路径=" + savePath)
if FileManager.default.fileExists(atPath: savePath) {
do {
print("删除重名文件")
try FileManager.default.removeItem(atPath: savePath)
}catch {
self.contentHandler!(self.bestAttemptContent!)
}
}
let saveUrl:URL = URL(fileURLWithPath: savePath)
do {
print("保存文件")
try FileManager.default.moveItem(at: result, to: saveUrl)
self.bestAttemptContent?.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: "wkw.wav"))
self.contentHandler!(self.bestAttemptContent!)
} catch{
self.contentHandler!(self.bestAttemptContent!)
}
}
}else{
self.contentHandler!(self.bestAttemptContent!)
}
}
task.resume()
}else{
self.contentHandler!(self.bestAttemptContent!)
}
}
func dealShowImgWithUserInfo(_ userInfo:[AnyHashable : Any]) {
if let urlStr = userInfo["thumb"] as? String, let url = URL(string: urlStr) {
let pathExtension = url.pathExtension
\
let task = URLSession.shared.downloadTask(with: url) { (result, response, error) in
if let result = result {
\
let identifier = ProcessInfo.processInfo.globallyUniqueString
let target = FileManager.default.temporaryDirectory.appendingPathComponent(identifier).appendingPathExtension(pathExtension)
\
do {
try FileManager.default.moveItem(at: result, to: target)
\
let attachment = try UNNotificationAttachment(identifier: identifier, url: target, options: nil)
self.bestAttemptContent?.attachments = [attachment]
self.contentHandler!(self.bestAttemptContent!)
}
catch {
print(error.localizedDescription)
self.contentHandler!(self.bestAttemptContent!)
}
}
}
task.resume()
}else{
self.contentHandler!(self.bestAttemptContent!)
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
}
}