我有一个用Swift编写的应用程序,它使用UNUserNotificationCenter,我让它在应用程序处于前台时给出通知。
我想要做的是,一旦通知被传递,应用程序就在前台更新UI --下面的通知显示得很好,但是当它出现时,我还想执行一个名为updateUI()的函数,因为通知日期在我的UI中,我想在通知出现时立即清除它。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}我不知道如何在完成处理程序中添加对updateUI()的调用。
发布于 2018-02-20 04:46:11
您可以POST来自AppDelegate的新通知,并在控制器文件中添加观察者以更改UI。
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
UIApplication.shared.applicationIconBadgeNumber = 0
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NewNotification") , object: nil, userInfo: response.notification.request.content.userInfo)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}在控制器文件中添加通知观察者:
NotificationCenter.default.addObserver(self, selector: #selector(pushNotificationHandler(_:)) , name: NSNotification.Name(rawValue: "NewNotification"), object: nil)然后调用UI更新方法:
func pushNotificationHandler(_ notification : NSNotification) {
self.updateUI()
}https://stackoverflow.com/questions/48876255
复制相似问题