Ios 我想在我的通知中显示图像,我尝试在扩展通知服务中这样做,但没有成功
确保以Ios 我想在我的通知中显示图像,我尝试在扩展通知服务中这样做,但没有成功,ios,swift,push-notification,Ios,Swift,Push Notification,确保以https发送图像。 示例:在NotificationService类中尝试此代码 override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler bestAttemptConten
https
发送图像。
示例:在
NotificationService
类中尝试此代码
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
if let attachmentString = bestAttemptContent.userInfo["url"] as? String,let attachmentURL = URL(string: attachmentString){
let session = URLSession(configuration: URLSessionConfiguration.default)
let dawnloadTask = session.downloadTask(with: attachmentURL, completionHandler:{ (url, _, error) in
if let error = error{
print(error)
}else if let url = url{
let attatchment = try! UNNotificationAttachment(identifier: attachmentString, url: url, options: [UNNotificationAttachmentOptionsTypeHintKey:kUnknownType])
bestAttemptContent.attachments=[attatchment]
}
contentHandler(bestAttemptContent)
})
dawnloadTask.resume()
}
}
}
“数据:{“名称”:“id”:“des”:“url”:“标题”:“正文”:“图像”:“}”,通知:{“标题”:“正文”:“图像”:“},url为此格式确保推送是以此格式从服务器端发送的。”。参见图。iOS在问题下方加了一条评论,我认为这是真的
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
func failEarly() {
contentHandler(request.content)
}
guard let attachmentURL = bestAttemptContent.userInfo["thumbnail"] as? String else {
return failEarly()
}
guard let url = URL(string: attachmentURL) else {
return failEarly()
}
do {
let imageData = try Data(contentsOf: url)
guard let attachment = UNNotificationAttachment.create(imageFileIdentifier: "image", data: imageData, options: nil) else {
return failEarly()
}
bestAttemptContent.attachments = [attachment]
contentHandler(bestAttemptContent.copy() as! UNNotificationContent)
}catch(let error) {
return failEarly()
}
}
}
extension UNNotificationAttachment {
static func create(imageFileIdentifier: String, data: Data, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
let tmpSubFolderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
do {
try fileManager.createDirectory(at: tmpSubFolderURL!, withIntermediateDirectories: true, attributes: nil)
let fileURL = tmpSubFolderURL?.appendingPathComponent(imageFileIdentifier)
try data.write(to: fileURL!, options: [])
let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL!, options: options)
return imageAttachment
} catch let error {
print("error \(error)")
}
return nil
}
}