'FCM Notifications are received in background but not foreground on iOS

I am getting FCM messages pushed to my iOS device fine when in the background but not in the foreground. I have found a number of articles where others had the same issue and solved it by adding the willPresentNotification function. However this isn't solving the issue for me. My FCM code is as follows:

class FCMManager: NSObject, MessagingDelegate, UNUserNotificationCenterDelegate {

static let shared = FCMManager()
let database = Database.database().reference()

public func registerForPushNotifications() {
    UNUserNotificationCenter.current().delegate = self
    let authOptions: UNAuthorizationOptions = [.alert, .sound, .announcement, .badge, .carPlay, .providesAppNotificationSettings]
    UNUserNotificationCenter.current()
        .requestAuthorization( options: authOptions,
                               completionHandler: { granted, _ in
            guard granted else {
                print("Apple push notifications are not registered")
                return
            }
            print("Apple push notifications are registered")
        })
    Messaging.messaging().delegate = self
    DispatchQueue.main.async {
        UIApplication.shared.registerForRemoteNotifications()
    }
    updatePushTokenIfNeeded()
 }

 func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
     updatePushTokenIfNeeded()
  }
}

My notification payloads are set up as:

 ["to" : "fcm_token",
  "notification" : ["title" : "Message Title",
                    "body" : "Message Body",
                    "sound" : "default",
                    "badge" : 1]
 ]

Then my app delegate is as follows:

class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {

  var window: UIWindow?
  let notificationCenter = UNUserNotificationCenter.current()

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      FirebaseApp.configure()
      let fcmManager = FCMManager()
      fcmManager.registerForPushNotifications()
      return true
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
      if #available(iOS 14, *) {
          completionHandler([.banner])
      } else {
          completionHandler([.alert])
      }
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source