【问题标题】:Keep a Firebase listener in memory when the app is in background当应用程序处于后台时,将 Firebase 侦听器保留在内存中
【发布时间】:2017-06-27 00:27:26
【问题描述】:

我一直在尝试使用 Firebase 侦听器来触发本地通知。我找到了一个post,它准确地解决了我试图解释的大部分内容,但是我没有对帖子发表评论的声誉,而且似乎没有迹象表明如何在其他任何地方完成我想要的.

原海报是这么说的。

我想通了!我不得不使用不同的方法,但我能够 让我的 Firebase 数据库观察者在 背景。

只要包含数据库观察者的对象不是 从内存中释放,它将继续观察和触发。所以我 创建了一个包含静态数据库对象的全局类 像这样的属性:

class GlobalDatabaseDelegate {
static let dataBase = DataBase()
 }

这是我对如何为自己的项目做什么感到困惑的地方。据我了解,我必须创建一个类似于 DataBase() 的类,其中包含我的数据库引用。问题是我不明白如何创建将包含数据库侦听器的类对象。

比如说我的参考是:

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

我想观察添加到数据库中的任何用户,然后触发本地通知。我可以编写代码来执行此操作,只是不确定如何将其包含在自己的对象类中,然后使其成为静态。

原谅我有点慢。任何帮助将不胜感激。

帖子的其余部分如下:

我还将 DataBase 类扩展为 UNUserNotificationCenterDelegate 以便它可以发送推送通知 像这样:

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

本质上,当应用程序处于后台时,我试图将 firebase 侦听器保留在内存中。

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    所以我链接的原始帖子有答案,但这是理解它的问题。我还以稍微不同的方法实现了我的代码。

    我发现另一篇文章详细介绍了运行自定义数据服务类所需的技术。 Custom Firebase Data Service Class : Swift 3

    要设置将 firebase 侦听器保留在内存中,只需几个步骤。

    1.创建一个firebase数据服务类。在那个类中,我有一个属于同一类的静态变量

    class FirebaseAPI {
        var isOpen = false
    
    static let sharedInstance = FirebaseAPI()
    
    //  I added functions for firebase reference in this class
    
    func observeNotifications(){
    
    //firebase call here
     }
    
    }
    

    2.在应用代理中设置通知设置。这是我的设置与原始帖子不同的地方。

     let notificationSettings = UIUserNotificationSettings(types: [.badge, .alert, .sound], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(notificationSettings)
    

    3.在您选择的视图控制器中创建对 firebase 类的引用,它适用于应用委托,但不建议使用。

    let sharedInstance = FirebaseAPI.sharedInstance
    

    4.调用函数设置观察者

    self.sharedInstance.observeNotifications()
    

    然后,您可以使用带有函数的完成处理程序触发本地通知,或者在 firebase 函数中触发通知。

    更新:Apple 已针对后台模式实施了更新,这些更新已停止此方法的工作。目前唯一的方法是使用APNS

    【讨论】:

    • 这不会将监听器保留在内存中
    • @Sente 抱歉,Apple 的更新已经停止了这项工作,几天前我遇到了同样的问题。我现在更新了帖子以反映它不再有效。
    • 值得一试。我将不得不学习使用 Firebase 推送通知
    • @Sente firebase.google.com/docs/functions/… 似乎是一个不错的起点。有可用的演示代码。
    猜你喜欢
    • 1970-01-01
    • 2017-05-31
    • 2020-09-17
    • 1970-01-01
    • 2019-08-10
    • 2017-03-10
    • 2020-01-25
    • 2019-05-16
    相关资源
    最近更新 更多