Firebase listener when app is in the background
Asked Answered
G

4

5

I have a Firebase childAdded event listener for the notifications table in my app that I would like to trigger push notifications when the app is in the background.

Here is the listener:

@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)")
        }
    }
}

This works great when the app is in the foreground. I then add a background observer in my app delegates applicationWillResignActive method to observe it in the background:

func applicationWillResignActive(_ application: UIApplication) {
     NotificationCenter.default.addObserver(self, selector: #selector(MainNavViewController.observeNotificationsChildAdded), name: notificationBackgroundProcessName, object: nil)
}

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:

Is it possible to send PushNotifications, using Firebase Cloud Messaging (FCM) to special UDID, directly from device?

however I'm not trying to send a push notification from one user to another, I'm trying to activate a UNNotificationRequest from within the database listener.

I also found this post: FCM background notifications not working in iOS but again, this differs from my use case because its being used within the FIRMessaging framework.

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?

Gove answered 16/1, 2017 at 21:35 Comment(0)
G
2

Devices have their own way of managing background network calls so handling background push notifications like this will not work on real devices. The observers are removed from memory shortly after the app goes into the background. I was able to solve this by creating a Node.js server which observes the notifications table and handles push notifications using the Firebase-Messenger framework. It was actually pretty easy. I used this blog post as my guide: Sending notifications between Android devices with Firebase Database and Cloud Messaging

This method will work however in the simulator (but not on real devices) so long as you pass the object containing the observer (the app delegate in this case) to background thread call like this:

func applicationWillResignActive(_ application: UIApplication) {
     NotificationCenter.default.addObserver(self,
                                            selector: #selector(self.observeNotificationsChildAddedBackground),
                                            name: notificationBackgroundProcessName,
                                            object: self)
}
Gove answered 31/1, 2017 at 22:23 Comment(1)
@MikeG when I tested this method on a real device the observer would be removed from memory shortly after the app goes into the background. Feel free to test yourself and share you’re resultsGove
G
3

I had the same problem and came across your post months ago. I have managed to finally understand what is required to observe notifications in the background.

I have detailed the solution that I am using in this post https://mcmap.net/q/1007457/-keep-a-firebase-listener-in-memory-when-the-app-is-in-background

The technique is as follows

  1. use a custom data service class
  2. initialise a sharedInstance of the data service class
  3. call the appropriate function in the data service class sharedInstance from your initial view controller.

This way the observer stays in memory

Update: Apple have implemented updates in regards to background modes which have stopped this method from working.

Goa answered 26/5, 2017 at 2:7 Comment(1)
@DustinSpengler Thanks, hope it is useful. Be aware of memory usage regarding firebase observers but otherwise have been using this system for a while now with great successGoa
G
2

Devices have their own way of managing background network calls so handling background push notifications like this will not work on real devices. The observers are removed from memory shortly after the app goes into the background. I was able to solve this by creating a Node.js server which observes the notifications table and handles push notifications using the Firebase-Messenger framework. It was actually pretty easy. I used this blog post as my guide: Sending notifications between Android devices with Firebase Database and Cloud Messaging

This method will work however in the simulator (but not on real devices) so long as you pass the object containing the observer (the app delegate in this case) to background thread call like this:

func applicationWillResignActive(_ application: UIApplication) {
     NotificationCenter.default.addObserver(self,
                                            selector: #selector(self.observeNotificationsChildAddedBackground),
                                            name: notificationBackgroundProcessName,
                                            object: self)
}
Gove answered 31/1, 2017 at 22:23 Comment(1)
@MikeG when I tested this method on a real device the observer would be removed from memory shortly after the app goes into the background. Feel free to test yourself and share you’re resultsGove
C
1

Simple solution would be to use background fetch. You can also use background fetch to periodically fetch the table for new entries and show a notification if a new row is added.

This does not assure that notifications are received immediately. But implementation is much simpler compared to using an APN.

Cyndy answered 24/4, 2017 at 22:12 Comment(1)
This is definitely a great option that i looked into. It didn't work for me personally because I needed the notifications to be live, where as this method causes the fetches to happen periodically.Gove
M
0

You can add background task to firebase observer

@objc func observeNotificationsChildAddedBackground() {

      var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid

      backgroundTask = UIApplication.shared.beginBackgroundTask {
           [unowned self] in
           UIApplication.shared.endBackgroundTask(backgroundTask)
           backgroundTask = UIBackgroundTaskInvalid
      }
      assert(backgroundTask != UIBackgroundTaskInvalid)

      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()
                   }
            }
         }
     })
}
Middlings answered 15/4, 2019 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.