UNNotificationServiceExtension偶尔无法更新通知

Kon*_*nov 7 iphone ios swift ios10 serviceextension

我有一个UNNotificationServiceExtension用Swift写的.它所做的一切:

  • 设置通知标题
  • 设置通知正文
  • 加载图片和通话 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!)
    }
}
Run Code Online (Sandbox Code Playgroud)

95%的情况下 - 它工作得很好.但是,偶尔通知到达时没有任何变化(即标题,正文保持不变,没有附加图像).

我知道:

  • 这不是超时:serviceExtensionTimeWillExpire没有被调用
  • 它看起来不像UNNotificationServiceExtension崩溃:我添加了大量的NSLog()电话来检查设备日志 - self.contentHandler!(self.bestAttemptContent!)火灾
  • 它经常发生在我的iPhone而不是iPad上
  • 我没有在设备日志中发现任何关于该问题的任何线索

有人遇到过这个问题吗?任何解决方法?思考?

JAL*_*JAL 0

UNNotificationServiceExtension在该功能首次发布时构建了一个。两个想法:

  1. 由于系统问题,底层URLSession数据任务无法获取远程媒体。获取 sysdiagnose 并查找 libnetwork.dylib 中的错误。

  2. 服务扩展是它自己独立的二进制文件和进程。系统可能未启动该进程,或者无法打开您的应用程序进程和服务扩展之间的链接。我还会检查 sysdaignose 中是否有任何表明进程的 mach 端口为 的内容nil