当应用程序处于后台时,将Firebase侦听器保留在内存中

我一直在尝试使用Firebase侦听器来触发本地通知。 我发现一篇文章正好说明了我正在尝试解决的大部分问题,但是我没有评论这篇文章的声望,似乎也没有任何迹象表明如何实现其他任何我想要的内容。

原来的海报说这个。

我想到了! 我必须使用不同的方法,但是我可以让我的Firebase数据库观察者在后台触发通知。

只要包含数据库观察者的对象没有从内存中释放,它将继续观察和触发。 所以我创build了一个包含静态数据库对象属性的全局类,如下所示:

class GlobalDatabaseDelegate { static let dataBase = DataBase() } 

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

举例来说,我的参考是:

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

我想观察添加到数据库的任何用户,然后触发本地通知。 我能够编写代码来做到这一点,只是不知道如何将其包含在自己的对象类,然后使其静态。

原谅我有点慢。 任何帮助将非常感激。

其余的post如下:

我还将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侦听器。

因此,我所链接的原文有答案,但这是理解它的问题。 我也用稍微不同的方法实现了我的代码。

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

设置保持内存中的Firebase侦听器有几个步骤。

1.创build一个firebase数据服务类。 在那个类中,我有一个类的静态variables

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

2.在应用程序委托中设置通知设置。 这是我的设置与原来的post不同的地方。

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

3.在你select的视图控制器中创build一个firebase类的引用,它在应用程序委托中工作,但不可取。

 let sharedInstance = FirebaseAPI.sharedInstance 

4.调用函数来设置观察者

 self.sharedInstance.observeNotifications() 

然后,您可以使用具有该function的完成处理程序触发本地通知,或在Firebasefunction中触发通知。

更新:苹果已经实施了关于后台模式的更新,这些模式已经停止了这种方法的工作。 目前唯一的方法是使用APNS