title: Flutter Push Notifications: Unable to receive notifications in background or when app is closed
body: <p>I am developing a flutter app for both Android and iOS. It has notifications, so I did implement the <a href="https://pub.dev/packages/firebase_messaging" rel="noreferrer">firebase_messaging</a> API. I am sending notifications to certain devices by device ID and sending notifications to <code>topics</code> as well.</p>
  <p>I am testing the <code>topics</code> notification sending, which is working 100% fine in Android. I followed the guide in above provided link, implemented iOS setups as well. But in iOS, when the notification is sent, it is being received by app only if it is in foreground. Which means, only via <code>onMessage</code>. If the app is in background or closed, I see no notification (I am printing it in console). But when I reopen the app, the notification gets printed.</p>
  <p>Below are my notification register code</p>
  <pre><code>    FirebaseMessagesImpl msg = FirebaseMessagesImpl();
           msg.requestPermissions();
      
      //Get configured with firebase messaging to recieve messages
      msg.getMessage().then((_) {
       msg.register().then((String token) {
       print(token);
      
       //Register to the `topic` so we get messages sent to the topic
       msg.topicRegister();
  .........
  }
  </code></pre>
  <p><strong>FirebaseMessagesImpl</strong></p>
  <pre><code>  import 'package:firebase_messaging/firebase_messaging.dart';
  
    import 'package:http/http.dart' as http;
    import 'dart:convert' as convert;
  
    class FirebaseMessagesImpl {
      final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  
      String serverToken =
          &quot;AAAAAAA:XXXXXXXXX-YYYYYYYY-ZZZZ-BBBBBBBB&quot;;
  
      Future&lt;String&gt; register() async {
        String token = &quot;&quot;;
        _firebaseMessaging.getToken().then((t) {
          token = t;
          print(&quot;ha: &quot; + t);
        });
  
        return token;
      }
  
      Future&lt;void&gt; getMessage() async {
        _firebaseMessaging.configure(
            onMessage: (Map&lt;String, dynamic&gt; message) async {
          print('on message $message');
          //setState(() =&gt; _message = message[&quot;notification&quot;][&quot;title&quot;]);
        }, onResume: (Map&lt;String, dynamic&gt; message) async {
          print('on resume $message');
          // setState(() =&gt; _message = message[&quot;notification&quot;][&quot;title&quot;]);
        }, onLaunch: (Map&lt;String, dynamic&gt; message) async {
          print('on launch $message');
          //setState(() =&gt; _message = message[&quot;notification&quot;][&quot;title&quot;]);
        });
      }
  
      void topicRegister() {
      // _firebaseMessaging.subscribeToTopic(&quot;mobile_admin&quot;);
        _firebaseMessaging.subscribeToTopic(&quot;puppies&quot;);
      }
  
  
  
      void requestPermissions() async
      {
        await _firebaseMessaging.requestNotificationPermissions(
          const IosNotificationSettings(sound:true, badge:true, alert:true, provisional:false)
        );
      }
  </code></pre>
  <p>}</p>
  <p>Below is my <strong>Info.plist</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;
  &lt;plist version=&quot;1.0&quot;&gt;
  &lt;dict&gt;
      &lt;key&gt;FirebaseAppDelegateProxyEnabled&lt;/key&gt;
      &lt;false/&gt;
      &lt;key&gt;CFBundleDevelopmentRegion&lt;/key&gt;
      &lt;string&gt;$(DEVELOPMENT_LANGUAGE)&lt;/string&gt;
      &lt;key&gt;CFBundleExecutable&lt;/key&gt;
      &lt;string&gt;$(EXECUTABLE_NAME)&lt;/string&gt;
      &lt;key&gt;CFBundleIdentifier&lt;/key&gt;
      &lt;string&gt;$(PRODUCT_BUNDLE_IDENTIFIER)&lt;/string&gt;
      &lt;key&gt;CFBundleInfoDictionaryVersion&lt;/key&gt;
      &lt;string&gt;6.0&lt;/string&gt;
      &lt;key&gt;CFBundleName&lt;/key&gt;
      &lt;string&gt;MY APP NAME&lt;/string&gt;
      &lt;key&gt;CFBundlePackageType&lt;/key&gt;
      &lt;string&gt;APPL&lt;/string&gt;
      &lt;key&gt;CFBundleShortVersionString&lt;/key&gt;
      &lt;string&gt;$(FLUTTER_BUILD_NAME)&lt;/string&gt;
      &lt;key&gt;CFBundleSignature&lt;/key&gt;
      &lt;string&gt;????&lt;/string&gt;
      &lt;key&gt;CFBundleVersion&lt;/key&gt;
      &lt;string&gt;$(FLUTTER_BUILD_NUMBER)&lt;/string&gt;
      &lt;key&gt;LSRequiresIPhoneOS&lt;/key&gt;
      &lt;true/&gt;
      &lt;key&gt;UIBackgroundModes&lt;/key&gt;
      &lt;array&gt;
          &lt;string&gt;fetch&lt;/string&gt;
          &lt;string&gt;remote-notification&lt;/string&gt;
      &lt;/array&gt;
      &lt;key&gt;UILaunchStoryboardName&lt;/key&gt;
      &lt;string&gt;LaunchScreen&lt;/string&gt;
      &lt;key&gt;UIMainStoryboardFile&lt;/key&gt;
      &lt;string&gt;Main&lt;/string&gt;
      &lt;key&gt;UISupportedInterfaceOrientations&lt;/key&gt;
      &lt;array&gt;
          &lt;string&gt;UIInterfaceOrientationPortrait&lt;/string&gt;
          &lt;string&gt;UIInterfaceOrientationLandscapeLeft&lt;/string&gt;
          &lt;string&gt;UIInterfaceOrientationLandscapeRight&lt;/string&gt;
      &lt;/array&gt;
      &lt;key&gt;UISupportedInterfaceOrientations~ipad&lt;/key&gt;
      &lt;array&gt;
          &lt;string&gt;UIInterfaceOrientationPortrait&lt;/string&gt;
          &lt;string&gt;UIInterfaceOrientationPortraitUpsideDown&lt;/string&gt;
          &lt;string&gt;UIInterfaceOrientationLandscapeLeft&lt;/string&gt;
          &lt;string&gt;UIInterfaceOrientationLandscapeRight&lt;/string&gt;
      &lt;/array&gt;
      &lt;key&gt;UIViewControllerBasedStatusBarAppearance&lt;/key&gt;
      &lt;false/&gt;
  &lt;/dict&gt;
  &lt;/plist&gt;
  </code></pre>
  <p>Below is <strong>AppDelegate.swift</strong></p>
  <pre><code>import UIKit
  import Flutter
  import Firebase
  
  @UIApplicationMain
  @objc class AppDelegate: FlutterAppDelegate {
    override func application(
      _ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -&gt; Bool {
      FirebaseApp.configure()
      GeneratedPluginRegistrant.register(with: self)
      if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
      }
      return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
      
      override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  
        Messaging.messaging().apnsToken = deviceToken
        super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
      }
  }
  </code></pre>
  <p>As some people recommended in OTHER SO answers, I removed the following section from the AppDelegate.swift code and tried, still the same issue persists.</p>
  <pre><code>if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
      }
  </code></pre>
  <p>I HAVE uploaded the apple key file to Firebase project <code>cloud messaging</code> section as in the guide as well.</p>
  <p>Below is my JSon code sending notifications</p>
  <pre><code>{
             &quot;notification&quot;: {
                &quot;body&quot;: &quot;body&quot;,
                &quot;title&quot;: &quot;title&quot;
             },
             &quot;priority&quot;: &quot;high&quot;,
             &quot;data&quot;: {
              &quot;body&quot;: &quot;body&quot;,
                &quot;title&quot;: &quot;title&quot;,
                &quot;click_action&quot;: &quot;FLUTTER_NOTIFICATION_CLICK&quot;,
                &quot;id&quot;: &quot;1&quot;,
                &quot;status&quot;: &quot;done&quot;,
                &quot;image&quot;: &quot;https://ibin.co/2t1lLdpfS06F.png&quot;
             },
             &quot;to&quot;: &quot;/topics/puppies&quot;
          }
  </code></pre>
  <p>My Signin Capabilities are as follows</p>
  <p><a href="https://i.stack.imgur.com/NjhpJ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/NjhpJ.png" alt="enter image description here" /></a>
  I am new to iOS and can't figure out whats going on. I thought just like in Android, the notification will auto appear in notification bar when the app is in background or closed.</p>
  <p><strong>Update</strong></p>
  <p>As suggestion by @nandish, I changed the <code>AppDelegate.swift</code> file as below</p>
  <pre><code>import UIKit
  import Flutter
  import Firebase
  
  @UIApplicationMain
  @objc class AppDelegate: FlutterAppDelegate, UNUserNotificationCenterDelegate {
    override func application(
      _ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -&gt; Bool {
      FirebaseApp.configure()
      GeneratedPluginRegistrant.register(with: self)
      UNUserNotificationCenter.current().delegate = self
      
      return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
      
      override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  
        Messaging.messaging().apnsToken = deviceToken
        super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
      }
      
      // MARK: - UNUserNotificationCenterDelegate Method
      func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -&gt; Void) {
          completionHandler([.alert])
      }
  
      func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -&gt; Void) {
  
       }
  }
  </code></pre>
  <p>Then I ended up with the following error</p>
  <pre><code>UNUserNotificationCenter' is only available in iOS 10.0 or newer
          func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -&gt; Void) {
  </code></pre>
  <p><strong>Update</strong></p>
  <p>In foreground, I am getting the following response from the app when a notification is issued. Thats also only in iPhone Simulator, not in real device.</p>
  <pre><code>{from: /topics/puppies, status: done, id: 1, notification: {body: body, title: title, e: 1, tag: topic_key_9204266xxxxxxx535}, title: title, image: https://ibin.co/2t1lLdpfS06F.png, collapse_key: com.aaa.xxx, body: body, click_action: FLUTTER_NOTIFICATION_CLICK}
  </code></pre>