title: FCM push notification not showing when app is open in swift 3
body: <p>I want to show fcm message any time if my app is open or not open.I want to catch message and save core data.when my app is opened I will get my message but notification alert doesn't show. But when I close my app it shows my message but I did not get my message in background.. here is my appDelegate</p>
  
  <pre><code>   @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate , UNUserNotificationCenterDelegate,MessagingDelegate{
  
     var window: UIWindow?
  
  
       func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {
      // Override point for customization after application launch.
  
      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 })
          // For iOS 10 data message (sent via FCM
          Messaging.messaging().remoteMessageDelegate = self
      } else {
          let settings: UIUserNotificationSettings =
              UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
          application.registerUserNotificationSettings(settings)
      }
  
      application.registerForRemoteNotifications()
  
      FirebaseApp.configure()
  
        return true
  }
  
  //MARK: FCM Token Refreshed
  func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
      // FCM token updated, update it on Backend Server
  }
  
  
  func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
      print("Messaging",messaging)
      print("remoteMessage*******************",remoteMessage)
  }
  
  
  func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -&gt; Void) {
      print("Handle push from foreground")
      // custom code to handle push while app is in the foreground
      print("\(notification.request.content.userInfo)")
  
  
      if UIApplication.shared.applicationState == .active { // In iOS 10 if app is in foreground do nothing.
          print("active****")
          completionHandler([])
      } else { // If app is not active you can show banner, sound and badge.
          print("Not active****")
          completionHandler([.alert, .badge, .sound])
      }
  
  }
  
  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -&gt; Void) {
      print("Handle push from background or closed")
      print("\(response.notification.request.content.userInfo)")
  
  }
  
  
  
  func tokenRefreshNotification(notification: NSNotification) {
      let refreshedToken = InstanceID.instanceID().token()
      //  print("fcm**************************","Dhruw: Connected to FCM. Token : \(String(describing: refreshedToken))")
      var fcm_token = String(describing: refreshedToken!)
  
  
      print("fcm toker********",fcm_token);
  
  
      connectToFCM()
  }
  
  func connectToFCM() {
      Messaging.messaging().shouldEstablishDirectChannel = true
  }
  
  
  
  func applicationWillResignActive(_ application: UIApplication) {
  
  }
  
  func applicationDidEnterBackground(_ application: UIApplication) {
  }
  
  func applicationWillEnterForeground(_ application: UIApplication) {
  
  }
  
  func applicationDidBecomeActive(_ application: UIApplication) {
  
  }
  
  func applicationWillTerminate(_ application: UIApplication) {
  
      self.saveContext()
  }
  
  // MARK: - Core Data stack
  
  lazy var persistentContainer: NSPersistentContainer = {
  
      let container = NSPersistentContainer(name: "Finjo_Expense_Management")
      container.loadPersistentStores(completionHandler: { (storeDescription, error) in
          if let error = error as NSError? {
  
              fatalError("Unresolved error \(error), \(error.userInfo)")
          }
      })
      return container
  }()
  
  
  
  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -&gt; Void) {
      guard let messageId = userInfo["gcm.message_id"]
          else {
  
              return
      }
  
      print(messageId)
  
      print("did*****",userInfo);
  
      let aps = userInfo[AnyHashable("aps")] as? NSDictionary
      var message = aps?["alert"] as! String;
      print("message",message)
  
  }
  
  // Called when APNs has assigned the device a unique token
  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
      // Convert token to string
      let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
  
      // Print it to console
      print("APNs device token: \(deviceTokenString)")
      //09256B251D3BBEAA50949067DF40AB75E5D2668170AB0C3B0D310205F2876C32
  
      // Persist it in your backend in case it's new
  }
  
  // Called when APNs failed to register the device for push notifications
  func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
      // Print the error to console (you should alert the user that registration failed)
      print("APNs registration failed: \(error)")
  }
  
  
  func saveContext () {
      let context = persistentContainer.viewContext
      if context.hasChanges {
          do {
              try context.save()
          } catch {
              // Replace this implementation with code to handle the error appropriately.
              // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
              let nserror = error as NSError
              fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
          }
         }
     }
  
    }
  </code></pre>
  
  <p>With Advance thanks...Please help me</p>