我用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%的情况下-它的工作非常好的。但是,occasionally通知到达时没有任何更改(即标题、正文保持不变、未附加图像)。
我知道:
serviceExtensionTimeWillExpire没有被调用UNNotificationServiceExtension崩溃:我添加了大量的NSLog()调用来检查设备日志--UNNotificationServiceExtension fires有人面对这个问题吗?有什么解决办法吗?有什么想法?
发布于 2018-09-10 02:01:40
当这个特性第一次被宣布时,我构建了一个UNNotificationServiceExtension。有两个想法:
URLSession数据任务无法获取远程媒体。获取系统诊断并查找libnetwork.dylib中的错误。nil。https://stackoverflow.com/questions/40162926
复制相似问题