Firebase listener when app is in the background

后端 未结 4 2099
逝去的感伤
逝去的感伤 2020-12-05 21:52

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

4条回答
  •  一个人的身影
    2020-12-05 22:37

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

提交回复
热议问题