当应用在后台时的Firebase监听器 [英] Firebase listener when app is in the background
问题描述
我的应用程序中的通知表的Firebase childAdded
事件侦听器,我希望在应用程序处于后台时触发推送通知。
以下是监听器:
$ b $ p $ b $ 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 newNotificationJSON = newNotificationJSON {
let status = newNotificationJSON [status ]
如果让status = status为?Int {
if status == 1 {
self.sendPushNotification()
}
}
}
func sendPushNotification(){
let content = UNMutableNotificationContent()
content.title =这是一个新的通知
content.subtitle =新通知推送
content.body =有人做了一些触发通知
content.sound = UNNotificationSound.default()
$ b $ let request = UNNotificationRequest(identifier:\(self.notificationBackgroundProcessName) ,content:content,trigger:nil)
NotificationCenter.default.post(name:notificationBackgroundProcessName,object:nil)
UNUserNotificationCenter.current()。delegate = self
UNUserNotificationCenter.current()。如果错误!= nil {
print(发送推送通知错误:\(error?.localizedDescription))
}
}
$ / code $ / pre
$ b $ p
当应用处于前台时,然后,在我的应用程序委托 applicationWillResignActive
方法中添加一个背景观察器,以便在后台观察它:
func applicationWillResignActive(_ application:UIApplication){
NotificationCenter.default.addObserver(self,selector:#selector(MainNavViewController.observeNotificationsChildAdded),name:notificationBackgroundProcessName,object:nil)
}
但是当应用程序在后台时事件观察者不会触发。我查看了Firebase Cloud Messenger来解决这个问题,并遇到类似这样的帖子:
可以发送推送通知然而,我不是试图从一个用户发送推送通知到另一个,我试图激活一个 UNNotificationRequest
来自数据库侦听器。
我有同样的问题,几个月前遇到你的帖子。我已经设法终于明白在后台观察通知需要什么。
我详细解释了我在这篇文章中使用的解决方案 https://stackoverflow.com/a/42240984/7048719
技巧如下使用自定义数据服务类
- 初始化数据服务类的共享实例
- 从您的初始视图控制器调用数据服务类
sharedInstance中的相应函数。 / li>
观察者留在记忆中
更新: / strong> Apple已经实现了关于已经停止这种方法的背景模式的更新。
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 background.
Here is the listener:
@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)")
}
}
}
This works great when the app is in the foreground. I then add a background observer in my app delegates applicationWillResignActive
method to observe it in the background:
func applicationWillResignActive(_ application: UIApplication) {
NotificationCenter.default.addObserver(self, selector: #selector(MainNavViewController.observeNotificationsChildAdded), name: notificationBackgroundProcessName, object: nil)
}
But the event observer doesn't fire when the app is in the background. Ive looked into Firebase Cloud Messenger to solve this problem and come across posts like this:
however I'm not trying to send a push notification from one user to another, I'm trying to activate a UNNotificationRequest
from within the database listener.
I also found this post:
FCM background notifications not working in iOS
but again, this differs from my use case because its being used within the FIRMessaging
framework.
So, is it possible to run this database listener when the app is in the background? If not, is it possible for me to make Firebase Cloud Messenger observe changes in a table and send a notification?
I had the same problem and came across your post months ago. I have managed to finally understand what is required to observe notifications in the background.
I have detailed the solution that I am using in this post https://stackoverflow.com/a/42240984/7048719
The technique is as follows
- use a custom data service class
- initialise a sharedInstance of the data service class
- call the appropriate function in the data service class sharedInstance from your initial view controller.
This way the observer stays in memory
Update: Apple have implemented updates in regards to background modes which have stopped this method from working.
这篇关于当应用在后台时的Firebase监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!