8

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?

JAL
  • 41,701
  • 23
  • 172
  • 300
Konstantin Loginov
  • 15,802
  • 5
  • 58
  • 95
  • Facing a similar issue. Have you resolved this? I'm thinking it's an iOS bug. – DanielRak Nov 05 '16 at 01:29
  • @DanielRak unfortunately not, still not resolved and I also suspect that it might be OS-bug. Please, if you find any solution for this one - don't hesitate to post it! – Konstantin Loginov Nov 05 '16 at 14:10
  • Absolutely! I've also failed to find any reliable way to reproduce it. Have you got any clue how to do that? – DanielRak Nov 05 '16 at 16:24
  • @DanielRak if you find repro-steps - post them also :-) I still don't see any pattern behind it – Konstantin Loginov Nov 05 '16 at 18:17
  • Hey just wondering...what push notification service provider are you using? Maybe it has something to do with how the payload is delivered or how the SDK is integrated within the app? – DanielRak Nov 08 '16 at 21:48
  • @DanielRak Good point! We're using *Urbanairship*. And you? – Konstantin Loginov Nov 08 '16 at 22:46
  • We're using OneSignal. I've contacted Apple about this and still haven't been able to figure it out unfortunately. – DanielRak Nov 17 '16 at 16:06
  • I have similar problem, but only with media attachments (I have described it: https://stackoverflow.com/q/45418450/4407087). It is not exactly what you posted, but maybe my info will help you. – rafalkitta Aug 01 '17 at 06:34
  • We are using Firebase and we have the same issue, it might be an issue with iOS... – Alexis.J Sep 11 '17 at 08:58
  • I am facing the same issue, any one get work around for this? – Megha Pawar Mar 05 '20 at 05:18
  • Ran into the same issue on iOS 13.3.1. Did you ever resolve this? And just as you said it was failing more often on the phone than the iPad. I'm still in the testing stage. Did you face this problem in Production as well? FWIW I was _told_ that you should 1. use `downloadTask` instead 2. setup your temp name before you fire off the download. Though I wonder how valid that is after seeing [this](https://stackoverflow.com/questions/45418450/unnotificationserviceextension-sometimes-doesnt-show-image). However for me the result of `data` from the `dataTask` was just sometimes `nil`. – mfaani Jun 05 '20 at 22:46
  • Also as simple as [this](https://stackoverflow.com/a/42834612/5175709) is, it might be the reason... – mfaani Jun 05 '20 at 22:50

1 Answers1

0

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.

JAL
  • 41,701
  • 23
  • 172
  • 300