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 ofNSLog()
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?