我有一个用Swift编写的UNNotificationServiceExtension
。它所做的一切:
contentHandler ()
这是我正在做的缩短版本:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
self.bestAttemptContent = request.content.mutableCopy() as? UNMutableNotificationContent
bestAttemptContent!.title = GetNotificationTitle(userInfo)
bestAttemptContent!.body = GetNotificationBody(userInfo)
if let imageUrl = <get image url> {
let imageLoaderTask = URLSession.shared.dataTask(with: URL.init(string: imageUrl)!) { (newsImageData, newsImageUrl, newsImageError) -> Void in
if newsImageError == nil && newsImageData != nil {
let documentDirectory = self.GetDocumentDirectory()
if documentDirectory != nil {
let newsFileURL = documentDirectory?.appendingPathComponent("news_image").appendingPathExtension("png")
do {
try newsImageData?.write(to: newsFileURL!)
let attachment = try UNNotificationAttachment(identifier: "newsImage",
url: newsFileURL!,
options: nil)
self.bestAttemptContent?.attachments = [ attachment, ]
self.contentHandler!(self.bestAttemptContent!)
return
} catch {}
}
}
self.contentHandler!(self.bestAttemptContent!)
}
imageLoaderTask.resume()
} else {
self.contentHandler!(self.bestAttemptContent!)
}
}
在95%的情况下 - 它工作得很好。但是,有时通知会在没有任何变化的情况下到达(即标题,正文保持不变,没有附加图像)。
我知道:
serviceExtensionTimeWillExpire
UNNotificationServiceExtension
崩溃:我添加了大量的NSLog()
调用来检查设备日志 - self.contentHandler!(self.bestAttemptContent!)
火灾有人遇到过这个问题吗?任何解决方法?思考?
该功能首次发布时,我建立了一个UNNotificationServiceExtension
。两个想法:
URLSession
数据任务无法获取远程媒体。抓住一个sysdiagnose并在libnetwork.dylib中查找错误。nil
。