title: Firebase listener when app is in the background
body: <p>I have a Firebase <code>childAdded</code> event listener for the notifications table in my app that I would like to trigger push notifications when the app is in the background. </p>
  
  <p>Here is the listener:</p>
  
  <pre><code>@objc func observeNotificationsChildAddedBackground() {
      self.notificationsBackgroundHandler = FIREBASE_REF!.child("notifications/\(Defaults.userUID!)")
      self.notificationsBackgroundHandler!.queryOrdered(byChild: "date").queryLimited(toLast: 99).observe(.childAdded, with: { snapshot in
          let newNotificationJSON = snapshot.value as? [String : Any]
          if let newNotificationJSON = newNotificationJSON {
              let status = newNotificationJSON["status"]
              if let status = status as? Int {
                  if status == 1 {
                      self.sendPushNotification()
                  }
              }
          }
      })
  }
  
  func sendPushNotification() {
      let content = UNMutableNotificationContent()
      content.title = "Here is a new notification"
      content.subtitle = "new notification push"
      content.body = "Someone did something which triggered a notification"
      content.sound = UNNotificationSound.default()
  
      let request = UNNotificationRequest(identifier: "\(self.notificationBackgroundProcessName)", content: content, trigger: nil)
      NotificationCenter.default.post(name: notificationBackgroundProcessName, object: nil)
      UNUserNotificationCenter.current().delegate = self
      UNUserNotificationCenter.current().add(request){ error in
          if error != nil {
              print("error sending a push notification :\(error?.localizedDescription)")
          }
      }
  }
  </code></pre>
  
  <p>This works great when the app is in the foreground. I then add a background observer in my app delegates <code>applicationWillResignActive</code> method to observe it in the background:</p>
  
  <pre><code>func applicationWillResignActive(_ application: UIApplication) {
       NotificationCenter.default.addObserver(self, selector: #selector(MainNavViewController.observeNotificationsChildAdded), name: notificationBackgroundProcessName, object: nil)
  }
  </code></pre>
  
  <p>But the event observer doesn't fire when the app is in the background. Ive looked into Firebase Cloud Messenger to solve this problem and come across posts like this: </p>
  
  <p><a href="https://stackoverflow.com/questions/37634046/is-it-possible-to-send-pushnotifications-using-firebase-cloud-messaging-fcm-t">Is it possible to send PushNotifications, using Firebase Cloud Messaging (FCM) to special UDID, directly from device?</a></p>
  
  <p>however I'm not trying to send a push notification from one user to another, I'm trying to activate a <code>UNNotificationRequest</code> from within the database listener. </p>
  
  <p>I also found this post:
  <a href="https://stackoverflow.com/questions/37899712/fcm-background-notifications-not-working-in-ios">FCM background notifications not working in iOS</a>
  but again, this differs from my use case because its being used within the <code>FIRMessaging</code> framework. </p>
  
  <p><strong>So, is it possible to run this database listener when the app is in the background? If not, is it possible for me to make Firebase Cloud Messenger observe changes in a table and send a notification?</strong></p>