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
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()
}
}
}
})
}