title: iOS Handle notifications and payload when app is in Background
body: <p>I'm developing an Swift iOS 14 app that send and recieves push notifications from Firebase Cloud Messaging.</p>
  <p>From FCM I send a message with a payload that must be treated by the app, updating an internal SQLite database with the payload data for later, show items in a view.</p>
  <p>When the app is in Foreground, I recieved the notification in the didReceiveRemoteNotification method and update the database but when the app is in Background or killed, the notification is recieved but no one method is called to handle the payload and update de database.</p>
  <p>I've read many topics about this problem but in none have I come to find a solution.</p>
  <p>At the moment I don't want to use an external database to insert the data, and later read the external database, but if there is no other options i will change the app (the reason is that i don't want to store any information out of the user application).</p>
  <p>My AppDelegate.swift is the following:</p>
  <pre><code>func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {
          //Firebase Auth + APNs
          FirebaseApp.configure()
          UNUserNotificationCenter.current().delegate = self
          let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
          UNUserNotificationCenter.current().requestAuthorization(
              options: authOptions,
              completionHandler: {_, _ in })
          application.registerForRemoteNotifications()
          Messaging.messaging().delegate = self
          return true
      }
  
  func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
      UserDefaults.standard.set(fcmToken, forKey: UserConstants.MESSAGING_TOKEN)
      let dataDict:[String: String] = [&quot;token&quot;: fcmToken]
        NotificationCenter.default.post(name: Notification.Name(&quot;FCMToken&quot;), object: nil, userInfo: dataDict)
  }
  
  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -&gt; Void) {
      if(userInfo[&quot;name&quot;] != nil) {
          ContactsService.sharedInstance.addAlert(phoneNumber: userInfo[&quot;phone&quot;] as! String, name: userInfo[&quot;name&quot;] as! String, isLocalized: Bool(userInfo[&quot;isLocalized&quot;] as? String), longitude: (userInfo[&quot;longitude&quot;] as! NSString).doubleValue, latitude: (userInfo[&quot;latitude&quot;] as! NSString).doubleValue)
      }
  }
  </code></pre>
  <p>Can someone help me, telling me if it's possible to do in that way or it's necessary to store the data externally to later retrieve it?</p>
  <p>Thank you!</p>