title: How to get push notification with FCM in background on iOS (Swift4)
body: <p>How to get push notification in background? I have a problem in Swift4 iOS 11 with push notifications with FCM unable to retrieve notifications in the background only when the application is opened.</p>
  <p>This is my coding:</p>
  <p>AppDelegate.swift</p>
  <pre><code>    @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
          
          var window: UIWindow?
          static var DEVICE_ID = String()
          
          func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {
              
              UIApplication.shared.statusBarStyle = .lightContent
              
              FirebaseApp.configure()
              
              if #available(iOS 10.0, *) {
                  // For iOS 10 display notification (sent via APNS)
                  UNUserNotificationCenter.current().delegate = self
                  
                  UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (success, error) in
                      print(success)
                  })
                  
                  Messaging.messaging().delegate = self
                  
              } else {
                  let settings: UIUserNotificationSettings =
                      UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
                  application.registerUserNotificationSettings(settings)
              }
              
              application.registerForRemoteNotifications()
              
              return true
          }
          
          func connectToFcm() {
              Messaging.messaging().shouldEstablishDirectChannel = true
              
              if let token = InstanceID.instanceID().token() {
                  AppDelegate.DEVICE_ID = token
                  print(&quot;*********&quot;)
                  print(&quot;Token Instance: \(token)&quot;)
                  print(&quot;*********&quot;)
              }
          }
          
          func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -&gt; Void) {
              
              completionHandler([.alert, .sound])
              
          }
          
          func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
              connectToFcm()
          }
          
          func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
              
              guard let data: [String: Any] = remoteMessage.appData as? [String: Any] else {
                  return
              }
              
              print(data)
              
           
          }
          
          func applicationDidBecomeActive(_ application: UIApplication) {
              UIApplication.shared.applicationIconBadgeNumber = 0
              connectToFcm()
          }
          
          func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -&gt; Void) {
              
              
              completionHandler(.newData)
              
          }
          
          
      }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/xIMHB.png" rel="nofollow noreferrer">Example: image send push notification with postman</a></p>