1. 富文本右图显示
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
let apsDic = request.content.userInfo["aps"] as! [String: Any]
let attachUrl = apsDic["image"]
// let category = apsDic["category"] //这里取消注释,需要在配置消息的时候加对应的category
// bestAttemptContent.categoryIdentifier = category as! String
// bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
let session = URLSession.shared
let url = URL.init(string: attachUrl as! String)!
let downloadTask: URLSessionDownloadTask = session.downloadTask(with: url) { (url, urlResponse, error) in
let caches = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).last
let file = "\(caches!)/\((urlResponse?.suggestedFilename)!)"
let mgr = FileManager.default
try! mgr.moveItem(atPath: url!.path, toPath: file)
if file != "" {
let attch = try! UNNotificationAttachment.init(identifier: "photo", url: URL.init(string: "file://\(file)")!, options: [:])
bestAttemptContent.attachments = [attch]
}
contentHandler(bestAttemptContent)
}
downloadTask.resume()
}
}
2. 右图支持http
在notificationService文件夹里面的info.plist里面用source code打开第五行添加:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>