UNNotificationServiceExtension偶尔无法更新通知

问题描述 投票:7回答:1

我有一个用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上
  • 我没有在设备日志中发现任何关于该问题的任何线索

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

ios iphone swift ios10 serviceextension
1个回答
0
投票

该功能首次发布时,我建立了一个UNNotificationServiceExtension。两个想法:

  1. 由于系统问题,底层的URLSession数据任务无法获取远程媒体。抓住一个sysdiagnose并在libnetwork.dylib中查找错误。
  2. 服务扩展是它自己独立的二进制和进程。系统可能没有启动该过程,或者无法在您的应用程序进程和服务扩展之间建立链接。我还要检查一个sysdaignose,看看这个过程的机器端口是nil
© www.soinside.com 2019 - 2024. All rights reserved.