title: Swift; HTTP request from UNUserNotificationCenter not working: The Internet connection appears to be offline
body: <p>I'm using <code>Xcode Version 9.4.1 (9F2000)</code>.</p>
  
  <p>I have this code in <code>AppDelegate.swift</code>:</p>
  
  <pre><code>func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {
      UNUserNotificationCenter.current().delegate = self
      showPushButtons()
      return true
  }
  func httpRequest(file: String, postKey1: String, postValue1: String, postKey2: String, postValue2: String) {
      let url = URL(string: "https://www.example.com/\(file)")!
      var request = URLRequest(url: url)
      request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
      request.httpMethod = "POST"
      let postString = "\(postKey1)=\(postValue1)&amp;\(postKey2)=\(postValue2)"
      request.httpBody = postString.data(using: .utf8)
      let task = URLSession.shared.dataTask(with: request) { data, response, error in
          guard let data = data, error == nil else {
              print("error=\(String(describing: error))")
              return
          }
          if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
              print("statusCode should be 200, but is \(httpStatus.statusCode)")
              print("response = \(String(describing: response))")
          }
  
          let responseString = String(data: data, encoding: .utf8)
          print("responseString = \(String(describing: responseString))")
      }
      task.resume()
  }
  func showPushButtons(){
      let replyAction = UNTextInputNotificationAction(
          identifier: "reply.action",
          title: "Reply to message",
          textInputButtonTitle: "Send",
          textInputPlaceholder: "Write some text here")
  
      let pushNotificationButtons = UNNotificationCategory(
          identifier: "allreply.action",
          actions: [replyAction],
          intentIdentifiers: [],
          options: [])
  
      UNUserNotificationCenter.current().setNotificationCategories([pushNotificationButtons])
  }
  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -&gt; Void) {
  
      if  response.actionIdentifier  ==  "reply.action" {
          if let textResponse =  response as? UNTextInputNotificationResponse {
              let sendText =  textResponse.userText
              print("Received text message: \(sendText)")
              httpRequest(file: "message.php", postKey1: "message", postValue1: "Hello!", postKey2: "chat_user", postValue2: "Peter")
          }
      }
      completionHandler()
  }
  </code></pre>
  
  <p><strong>What it does:</strong></p>
  
  <p>When receiving a push notification and making a force touch, a textfield and the keyboard will appear (as known from messaging apps like WhatsApp). You can write some text and submit/send it.</p>
  
  <p><strong>You can get and print that submitted message with this line:</strong></p>
  
  <p><code>print("Received text message: \(sendText)")</code></p>
  
  <p>This is working without any problems.</p>
  
  <p>But when trying to send the data to my server like this:</p>
  
  <pre><code>httpRequest(file: "message.php", postKey1: "message", postValue1: "Hello!", postKey2: "chat_user", postValue2: "David")
  </code></pre>
  
  <p>it's not working. There's no access to my server and I'm getting errors like this in console log:</p>
  
  <blockquote>
    <p>Received text message: First try</p>
    
    <p>2018-07-19 08:45:00.643935+0200 MyApp[4307:1502538] +[CATransaction
    synchronize] called within transaction</p>
    
    <p>2018-07-19 08:45:00.644639+0200 MyApp[4307:1502538] +[CATransaction
    synchronize] called within transaction</p>
    
    <p>2018-07-19 08:45:13.091958+0200 MyApp[4307:1502647] TIC TCP Conn
    Failed [1:0x1c4169a80]: 1:50 Err(50)</p>
    
    <p>2018-07-19 08:45:13.093089+0200 MyApp[4307:1502647] Task
    &lt;1E8151BB-7098-46CD-9F68-8AA0E320CB7D>.&lt;1> HTTP load failed (error
    code: -1009 [1:50])</p>
    
    <p>Received text message: Second try</p>
    
    <p>2018-07-19 08:45:13.094756+0200 MyApp[4307:1503029] Task
    &lt;1E8151BB-7098-46CD-9F68-8AA0E320CB7D>.&lt;1> finished with error - code:
    -1009</p>
    
    <p>2018-07-19 08:45:13.096208+0200 MyApp[4307:1502538] +[CATransaction
    synchronize] called within transaction</p>
    
    <p>2018-07-19 08:45:13.096580+0200 MyApp[4307:1502538] +[CATransaction
    synchronize] called within transaction error=Optional(Error
    Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to
    be offline." UserInfo={NSUnderlyingError=0x1cc047320 {Error
    Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)"
    UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}},
    NSErrorFailingURLStringKey=<a href="https://www.example.com/message.php" rel="nofollow noreferrer">https://www.example.com/message.php</a>,
    NSErrorFailingURLKey=<a href="https://www.example.com/message.php" rel="nofollow noreferrer">https://www.example.com/message.php</a>,
    _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=50, NSLocalizedDescription=The Internet connection appears to be
    offline.})</p>
  </blockquote>
  
  <p>My function <code>httpRequest()</code> seems to work because I can e.g. call it from <code>didFinishLaunchingWithOptions</code> like this:</p>
  
  <pre><code>httpRequest(file: "message.php", postKey1: "message", postValue1: "Hello!", postKey2: "chat_user", postValue2: "David")
  </code></pre>
  
  <p>without any problem. That also means that my domain and my server are working fine.</p>
  
  <p>But why can't I call my <code>httpRequest()</code> function from my <code>UNUserNotificationCenter</code> function?</p>
  
  <p>When receiving a push notification, my app is of course in background or closed. Do I need some special code to make it work in background mode or so?</p>