应用在后台时的Firebase监听器

我的应用中的通知表有一个Firebase childAdded事件监听器,我想在应用程序在后台时触发推送通知。

这是听众:

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

当应用程序位于前台时,这非常有用。 然后我在我的应用程序委托applicationWillResignActive方法中添加一个背景观察器,以在后台观察它:

 func applicationWillResignActive(_ application: UIApplication) { NotificationCenter.default.addObserver(self, selector: #selector(MainNavViewController.observeNotificationsChildAdded), name: notificationBackgroundProcessName, object: nil) } 

但是当应用程序在后台时,事件观察者不会触发。 我调查了Firebase Cloud Messenger来解决这个问题并遇到这样的post:

是否可以直接从设备使用Firebase云消息传递(FCM)向特殊UDID发送PushNotifications?

但是我并没有尝试从一个用户向另一个用户发送推送通知,我正在尝试从数据库侦听器中激活UNNotificationRequest

我还发现这篇文章: FCM背景通知在iOS中不起作用但是,这与我的用例不同,因为它在FIRMessaging框架中使用。

那么,当应用程序在后台运行时,是否可以运行此数据库侦听器? 如果没有,我是否可以让Firebase Cloud Messenger观察表中的更改并发送通知?

我遇到了同样的问题,几个月前发现了你的post。 我已经设法最终了解在后台观察通知所需的内容。

我已经详细介绍了我在这篇文章中使用的解决方案https://stackoverflow.com/a/42240984/7048719

该技术如下

  1. 使用自定义数据服务类
  2. 初始化数据服务类的sharedInstance
  3. 从初始视图控制器调用数据服务类sharedInstance中的相应函数。

这样观察者就会留在记忆中

更新: Apple已经实施了有关后台模式的更新,这些更新已停止此方法的工作。

设备有自己的方式来管理后台网络呼叫,因此处理这样的后台推送通知将无法在真实设备上运行。 在应用程序进入后台后不久,观察者将从内存中删除。 我能够通过创建一个Node.js服务器来解决这个问题,该服务器观察notifications表并使用Firebase-Messenger框架处理推送通知。 这实际上非常简单。 我使用此博客文章作为我的指南: 在Android设备与Firebase数据库和云消息传递之间发送通知

然而,只要将包含观察者的对象(在本例中为app委托)传递给后台线程调用,此方法将在模拟器中工作( 但不在实际设备上 ):

 func applicationWillResignActive(_ application: UIApplication) { NotificationCenter.default.addObserver(self, selector: #selector(self.observeNotificationsChildAddedBackground), name: notificationBackgroundProcessName, object: self) } 

简单的解决方案是使用后台提取。 您还可以使用后台提取来定期获取新条目的表,并在添加新行时显示通知。

这并不能确保立即收到通知。 但与使用APN相比,实现要简单得多。