Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

ios - Keep a Firebase listener in memory when the app is in background

I have been trying to use a Firebase listener to trigger local notifications. I have found a post that addresses exactly what I am trying to do with much of it explained, however I do not have the reputation to comment on the post and there seems to be no indication of how to accomplish what I want anywhere else.

The original poster says this.

I figured it out! I had to use a different approach but i was able to get my Firebase Database observer to trigger notifications in the background.

As long as the object containting the database observer is not deallocated from memory it will continue to observe and trigger. So I created a global class which contains a static database object property like this:

class GlobalDatabaseDelegate {
static let dataBase = DataBase()
 }

This is where I am confused as to what to do for my own project. It is my understanding that I have to create a class similar to DataBase() which contains my database reference. The problem is I do not understand how to create class object that will contain the database listener.

say for example my reference is :

let userRef = FIRDatabase.database.reference().child("users")

And I want to observe any users added to the database and then trigger a local notification. I am able to write the code to do so, just not sure how to contain it in an object class of its own and then make it static.

Forgive me for being a little slow. Any help would be very much appreciated.

The rest of the post follows :

I also extended the DataBase class to be the UNUserNotificationCenterDelegate so it can send the push notitications like this:

    extension DataBase: UNUserNotificationCenterDelegate {

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("Tapped in notification")
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("Notification being triggered")
        completionHandler( [.alert,.sound,.badge])
    }

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

In essence I am trying to keep a firebase listener in memory when the app is in background.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...