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 backgro
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.
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)
}
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://stackoverflow.com/a/42240984/7048719
The technique is as follows
This way the observer stays in memory
Update: Apple have implemented updates in regards to background modes which have stopped this method from working.
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()
}
}
}
})
}