title: Push Notification in Background not working iOS12 Swift4.2
body: <p>I have implemented push notification by firebase. Not able to get notifications from background. Having following code in my App delegate,</p>
  
  <pre><code>class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {
  
      var window: UIWindow?
       let gcmMessageIDKey = "gcm.message_id"
  
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {
           sleep(3)
          IQKeyboardManager.shared.enable = true
  
          if #available(iOS 10.0, *) {
              // For iOS 10 display notification (sent via APNS)
              UNUserNotificationCenter.current().delegate = self
  
              let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
              UNUserNotificationCenter.current().requestAuthorization(
                  options: authOptions,
                  completionHandler: {_, _ in })
          } else {
              let settings: UIUserNotificationSettings =
                  UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
              application.registerUserNotificationSettings(settings)
          }
          Messaging.messaging().delegate = self
      Messaging.messaging().shouldEstablishDirectChannel = true
  
        UIApplication.shared.registerForRemoteNotifications()
    FirebaseApp.configure()
  
          return true
      }
  
      func applicationWillResignActive(_ application: UIApplication) {
  
      }
  
      func applicationDidEnterBackground(_ application: UIApplication) {
          Messaging.messaging().shouldEstablishDirectChannel = true
  
      }
  
      func applicationWillEnterForeground(_ application: UIApplication) {
  
      }
  
      func applicationDidBecomeActive(_ application: UIApplication) {
  
      }
  
      func applicationWillTerminate(_ application: UIApplication) {
  
      }
      func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
          print("Firebase registration token: \(fcmToken)")
  
          let dataDict:[String: String] = ["token": fcmToken]
          NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
          // TODO: If necessary send token to application server.
          // Note: This callback is fired at each app startup and whenever a new token is generated.
      }
      func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
          print("Message Data", remoteMessage.appData)
  
      }
      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
          // If you are receiving a notification message while your app is in the background,
          // this callback will not be fired till the user taps on the notification launching the application.
          // TODO: Handle data of notification
  
          // With swizzling disabled you must let Messaging know about the message, for Analytics
          // Messaging.messaging().appDidReceiveMessage(userInfo)
  
          // Print message ID.
          if let messageID = userInfo[gcmMessageIDKey] {
              print("Message ID: \(messageID)")
          }
  
          // Print full message.
          print(userInfo)
      }
  
      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                       fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -&gt; Void) {
          // If you are receiving a notification message while your app is in the background,
          // this callback will not be fired till the user taps on the notification launching the application.
          // TODO: Handle data of notification
  
          // With swizzling disabled you must let Messaging know about the message, for Analytics
          // Messaging.messaging().appDidReceiveMessage(userInfo)
  
          // Print message ID.
          if let messageID = userInfo[gcmMessageIDKey] {
              print("Message ID: \(messageID)")
          }
  
          // Print full message.
          print(userInfo)
  
          completionHandler(UIBackgroundFetchResult.newData)
      }
      // Receive displayed notifications for iOS 10 devices.
      func userNotificationCenter(_ center: UNUserNotificationCenter,
                                  willPresent notification: UNNotification,
                                  withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -&gt; Void) {
          let userInfo = notification.request.content.userInfo
  
          // With swizzling disabled you must let Messaging know about the message, for Analytics
          // Messaging.messaging().appDidReceiveMessage(userInfo)
  
          // Print message ID.
          if let messageID = userInfo[gcmMessageIDKey] {
              print("Message ID: \(messageID)")
          }
  
          // Print full message.
          print(userInfo)
  
          // Change this to your preferred presentation option
          completionHandler([.alert])
      }
  
      func userNotificationCenter(_ center: UNUserNotificationCenter,
                                  didReceive response: UNNotificationResponse,
                                  withCompletionHandler completionHandler: @escaping () -&gt; Void) {
          let userInfo = response.notification.request.content.userInfo
          // Print message ID.
          if let messageID = userInfo[gcmMessageIDKey] {
              print("Message ID: \(messageID)")
          }
  
          // Print full message.
          print(userInfo)
  
          completionHandler()
      }
  
  }
  </code></pre>
  
  <p>And getting print for Message Data at did receive remote Notification</p>
  
  <pre><code>Message Data [AnyHashable("from"): 459345502087, AnyHashable("collapse_key"): www.rwdev.com.TravelSheriff2.1, AnyHashable("notification"): {
      body = "Notifications ";
      e = 1;
      tag = "campaign_collapse_key_4509884114250587926";
      title = "Travel Sheriff ";
  }]
  </code></pre>
  
  <p>But I am not able to get push notifications while background and foreground. Even not getting "Message Data" print while app in background.
  (Note:Please don't come up with previous stack overflow links and answers have already tried most of them..didnt work out..)</p>