title: Logging from Notification Service Extension
body: <p>So currently I'm doing this: </p>
  
  <pre><code>override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -&gt; Void) {
      logger = AnalyticsManager.shared
      self.contentHandler = contentHandler
      bestAttemptContent = (request.content.mutableCopy() as! UNMutableNotificationContent)
      if let notificationContent = bestAttemptContent {
          extensionHandler = ServiceExtensionHandler(notificationContent: notificationContent, logger: AnalyticsManager.shared)
          extensionHandler?.handleNotificationRequest { modifiedNotificationContent in
              guard let modifiedNotificationContent = modifiedNotificationContent else {
                  contentHandler(notificationContent)
                  return
              }
              contentHandler(modifiedNotificationContent)
          }
      } else {
          contentHandler(request.content)
          return
      }
  }
  </code></pre>
  
  <p>In my <code>ServiceExtensionHandler</code> if image download succeeds, I'll return the modified notification. So far so good. </p>
  
  <p>But once the image download succeeds, I want to log an event.
  Problem is if I return the <code>contentHandler</code> then the OS will kill the extension and I won't have time to complete my log. </p>
  
  <blockquote>
    <p>Shortly after the app extension performs its task (or starts a
    background session to perform it), the system terminates the
    extension.</p>
    
    <p><sub>Docs: <a href="https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html" rel="nofollow noreferrer">An App Extension’s Life Cycle</a> </sub></p>
  </blockquote>
  
  <p>At the moment it's working but that extra network call isn't guaranteed to work. </p>
  
  <p>I could only return that completionHandler if the log returns, but then well the log could timeout in 60 seconds and that's another problem in itself as well. </p>
  
  <p>Has anyone thought of any solutions for this?</p>