title: iOS Swift Twilio Programmable Chat Push Notifications
body: <p>Using the TwilioChatClient pod, I have successfully registered my app to Twilio <a href="https://www.twilio.com/docs/tutorials/walkthrough/chat/ios/swift" rel="nofollow noreferrer">Programmable Chat</a> to receive APN notifications. </p>
  
  <p>However, from what I can tell, these notifications are being created after calling <code>client.register(withToken: deviceToken)</code> on an instantiated TwilioChatClient client, and NOT through the application's AppDelegate <code>didReceiveRemoteNotification</code> method. Stranger yet, <code>didReceiveRemoteNotification</code> is called, but only when the application is in the active state, and not the background state, where I would like to perform some operations.</p>
  
  <p>Does anyone know where and how these notifications are being created, or why <code>didReceiveRemoteNotification</code> is only called during the active state? Amongst other things, I would like to increment the application icon badge number with each notification sent out by the client.</p>
  
  <pre><code>func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
      print("Registered for notifications");
      if UserUtils.client?.userInfo != nil {
          print("Has info");
          UserUtils.deviceToken = deviceToken;
          UserUtils.client?.register(withToken: deviceToken)
      } else {
          print("No info");
          updatedPushToken = deviceToken as NSData?
      }
  }
  
  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -&gt; Void) {
      print("Received a notification");
      if UIApplication.shared.applicationState != .active {
          print(userInfo);
          UserUtils.client?.handleNotification(userInfo);
          UIApplication.shared.applicationIconBadgeNumber += 1;
          if UserUtils.client?.userInfo != nil {
              print(userInfo);
              let jsonNotification = JSON(userInfo["aps"])
              let alert = jsonNotification["alert"].stringValue + "\"}";
              print(JSON.init(parseJSON: alert)["body"]);
          } else {
              print(userInfo);
              let jsonNotification = JSON(userInfo["aps"])
              let alert = jsonNotification["alert"].stringValue + "\"}";
              print(JSON.init(parseJSON: alert)["body"]);
          }
      } else {
      }
  }
  </code></pre>
  
  <p>where the <code>client.register(withToken: deviceToken)</code> works as intended.</p>