title: How to show image in UNNotificationServiceExtension?
body: <p>I added the <code>UNNotificationServiceExtension</code> to the application, I successfully get the data, and I load the image that comes in the data to push notification, but I can not display this image in the push notification window. Please tell me how it can be fixed?</p>
  
  <pre><code>override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -&gt; Void) {
          self.contentHandler = contentHandler
          bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
  
          if let bestAttemptContent = bestAttemptContent {
              // Modify the notification content here...
              bestAttemptContent.title = "Apple [modified]"
              bestAttemptContent.body = "Xcode"
              contentHandler(bestAttemptContent)
  
              let attachmentStorage = AttachmentStorage()
  
              if let imagePath = request.content.userInfo["image"] as? String {
                  guard let url = URL(string: imagePath) else {
                      contentHandler(bestAttemptContent)
                      return
                  }
                  debugPrint("url", url)
                  attachmentStorage.store(url: url) { (localURL, error) in
                      if let localURL = localURL {
                          debugPrint("path", localURL)
                          do {
                              let attachment = try UNNotificationAttachment.init(identifier: "image", url: localURL, options: nil)
                              bestAttemptContent.attachments = [attachment]
                              debugPrint("bestAttemptContent.attachments", bestAttemptContent.attachments, bestAttemptContent.attachments.count)
                              contentHandler(bestAttemptContent)
                              return
                          } catch {
                              contentHandler(bestAttemptContent)
                              return
                          }
                      }
                  }
              }
  
  
              contentHandler(bestAttemptContent)
          }
      }
  
  class AttachmentStorage {
  
      func store(url: URL, completion: ((URL?, Error?) -&gt; ())?) {
          // obtain path to temporary file
          let filename = ProcessInfo.processInfo.globallyUniqueString
          let path = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(filename).jpeg")
  
          // fetch attachment
          let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
              do {
                  let _ = try data?.write(to: path)
                  completion?(path, error)
              } catch {
                  debugPrint(error.localizedDescription)
                  completion?(nil, error)
              }
          }
          task.resume()
      }
  
  }
  </code></pre>
  
  <p>Logs </p>
  
  <pre><code>"bestAttemptContent.attachments" [&lt;UNNotificationAttachment: 0x14dd626c0; identifier: image, family: Image, URL: file:///private/var/mobile/Containers/Data/PluginKitPlugin/1F13F774-B6CA-4567-9EF0-EA61C9A1F2A0/tmp/23A8E7EC-29FB-4C36-893D-5B0DCFB711B2-13808-000002A699FBF67B.jpeg, type: public.jpeg, options: &lt;UNImageNotificationAttachmentOptions: 0x14dd5afd0&gt;&gt;] 1
  </code></pre>
  
  <p>I added this code. And even if you remove contentHandler sometimes you get duplicates or there were no images</p>
  
  <pre><code>let request = UNNotificationRequest.init(identifier: "image", content: bestAttemptContent, trigger: nil)
  UNUserNotificationCenter.current().add(request) { (error) in
                                  // handle error
                              }
  </code></pre>