title: Flutter - FCM Remote/Silent Notification (app in background)
body: <p>I have a Flutter app that needs to receive FCM push messages (silent/background data message). I am not using Flutter's plugin for Firebase Cloud Messaging, as it does not support background mode currently. So I have integrated FCM IOS sdk into the project as per the Firebase documentation for IOS.</p>
  <p>I can receive &quot;notification&quot; messages fine (where notification is shown to the user when a push message arrives). So I believe my setup (keys/fcm) is good.</p>
  <p>The problem now is, when I send a data message (dont want the app to show a notification to the user), nothing happens.</p>
  <p>My message looks like</p>
  <pre><code>{
    &quot;to&quot;: &quot;fcm-token&quot;,
    &quot;content_available&quot;: true,
    &quot;apns-priority&quot;: 5,
    &quot;data&quot;: {
      &quot;some_key&quot;: &quot;some_value&quot;
    }
  } 
  </code></pre>
  <p>I am expecting the data message above to trigger the following delegate method (but it does not).</p>
  <pre><code>override func application(_ application: UIApplication,
                                didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                                fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -&gt; Void)
  </code></pre>
  <p>I have tried enabling or disabling method swizzling but that didnt help either.</p>
  <p>Following is how I setup delegate for FCM messaging</p>
  <pre><code>extension AppDelegate : MessagingDelegate {
      override func application(_ application: UIApplication,
                                didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                                fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -&gt; Void){
          print(&quot;didReceiveRemoteNotification&quot;)
          completionHandler(.newData)
      }
      
      func initFirebase(_ application: UIApplication) {
          FirebaseApp.configure()
          Messaging.messaging().delegate = self
          
          if #available(iOS 10.0, *) {
              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)
          }
          
          application.registerForRemoteNotifications()
      }
      
      @available(iOS 10.0, *)
      override func userNotificationCenter(_ center: UNUserNotificationCenter,
                                           willPresent notification: UNNotification,
                                           withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -&gt; Void) {
  
          let userInfo = notification.request.content.userInfo
          completionHandler([[.alert, .sound]])
      }
      
      @available(iOS 10.0, *)
      override func userNotificationCenter(_ center: UNUserNotificationCenter,
                                           didReceive response: UNNotificationResponse,
                                           withCompletionHandler completionHandler: @escaping () -&gt; Void) {
          
          print(&quot;withCompletionHandler&quot;)
          completionHandler()
      }
      
      override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
          print(&quot;didFailToRegisterForRemoteNotificationsWithError&quot;)
      }
      
      func application(application: UIApplication,
                       didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
          
          
          print(&quot;didRegisterForRemoteNotificationsWithDeviceToken&quot;)
      }
      
      func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
          print(&quot;didReceiveRegistrationToken&quot;)
      }
      
      func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
              print(&quot;Received data message: \(remoteMessage.appData)&quot;)
      }
  }
  </code></pre>
  <p>Any idea why the delegates are not getting called for data messages???</p>
  <p>P.S: Ofcourse I am testing on a real IOS device</p>