UNNotificationServiceExtension fails to update notifications occasionally

1.1k Views Asked by At

I have a UNNotificationServiceExtension written in Swift. All it does:

  • Set notification title
  • Set notification body
  • Load image & call contentHandler ()

Here's a shorted version of what am I doing:

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!)
    }
}

In 95% cases - it works just fine. However, occasionally notification arrives without any changes (i.e. title, body remained the same, no image has been attached).

I know that:

  • It's not timeout: serviceExtensionTimeWillExpire is not called
  • It doesn't look like UNNotificationServiceExtension crashes: I've added plenty of NSLog() calls to check Device Logs - self.contentHandler!(self.bestAttemptContent!) fires
  • It happens more often on my iPhone rather than on iPad
  • I haven't found any single clue in Device Logs regarding the issue

Does anyone faced this issue? Any workarounds? Thoughts?

1

There are 1 best solutions below

0
JAL On

I built a UNNotificationServiceExtension when the feature was first announced. Two ideas:

  1. The underlying URLSession data task is failing to fetch the remote media due to a system issue. Grab a sysdiagnose and look for errors in libnetwork.dylib.

  2. The service extension is its own separate binary and process. It's possible that the system did not launch the process, or could not open up a link between your application process and the service extension. I'd also check a sysdaignose for anything that says the mach port of the process is nil.