UNNotificationServiceExtension偶尔更新通知失败



我有一个用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!)火灾
  • 这在我的iPhone上比在iPad上更常见
  • 我在设备日志中没有发现任何关于这个问题的线索

有人遇到过这个问题吗?有解决方法吗?想法吗?

在该特性首次发布时,我构建了一个UNNotificationServiceExtension。两个概念:

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

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

最新更新