我想在表视图更新时通知用户。我正在使用firebase但我想在tableview更新时发送本地通知。我不知道如何设置tableview更新时的触发器。我阅读了所有可以阅读的文档,我无法弄明白。我的代码链接在下面以供参考我设置通知。我只是不知道如何在tableview更新时添加通知。
import UIKit
import FirebaseAuth
import FirebaseDatabase
import UserNotifications
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var ref: DatabaseReference!
var databaseHandle:DatabaseHandle?
var postData = [String]()
override func viewWillLayoutSubviews() {
self.navigationController?.isNavigationBarHidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: {didAllow, error in
})
ref = Database.database().reference()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
databaseHandle = ref.child("Posts").observe(.childAdded) { (snapshot) in
let post = snapshot.value as? String
if let actualPost = post {
self.postData.append(actualPost)
self.tableView.reloadData()
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell")
cell?.textLabel?.text = postData[indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCellEditingStyle.delete) {
// handle delete (by removing the data from your array and updating the tableview)
Database.database().reference().removeValue()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .none
}
答案 0 :(得分:0)
你viewDidLoad
应该是这样的:
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: {didAllow, error in
})
ref = Database.database().reference()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
databaseHandle = ref.child("Posts").observe(.childAdded) { (snapshot) in
let post = snapshot.value as? String
if let actualPost = post {
self.postData.append(actualPost)
self.tableView.reloadData()
let content = UNMutableNotificationContent()
content.title = "Don't forget"
content.body = "The table view is up to date !"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "UYLReminderCategory"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1,
repeats: false)
UNUserNotificationCenter.current().add(.init(identifier: "myNot", content: content, trigger: trigger)) { (error) in
//Your notification completion handler
}
}
}
}
我认为您已经处理了通知授权。 然后,每次Firebase观察者向表格视图触发新数据时,您都会触发通知。
答案 1 :(得分:0)
尝试一下。
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: {didAllow, error in
})
ref = Database.database().reference()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
databaseHandle = ref.child("Posts").observe(.childAdded) { (snapshot) in
let post = snapshot.value as? String
if let actualPost = post {
self.postData.append(actualPost)
self.reload(completion: {
//local notification here
})
}
}
}
func reload(completion:() -> Void) {
self.tableView.reloadData()
completion()
}