我尝试从本地通知中打开特定的View控制器..我在 AppDelegate 中添加了操作和类别以及 scheduleNotification的功能。 当我尝试打开任何ViewController(故事板)时,每件事似乎都好! 除非打开ViewController(storyboard),它具有发送通知功能..
当我评论它时,这是ViewController中的函数,它工作!:
func expiredDate() {
print("start")
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd" //format the date to match how it's stored in Firebase
let ref = FIRDatabase.database().reference(withPath: "/Data/\(user!.uid)/")
ref.child("EssentialsList").observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children {
let snap = child as! FIRDataSnapshot
let dict = snap.value as! [String: Any]
let eventDateStampString = dict["date"] as! String
let endDate = formatter.date(from: eventDateStampString)
let calendar = NSCalendar.current as NSCalendar
var comps = DateComponents()
comps.day = -5
let exDate = calendar.date(byAdding: comps,
to: endDate! , options: [])
print(" expired date: \(String(describing: exDate))")
let delegate = UIApplication.shared.delegate as? AppDelegate
delegate?.scheduleNotification(at: exDate!)
}
})
}
这里是AppDelegate中句柄Action的代码:
extension AppDelegate: UNUserNotificationCenterDelegate {
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.actionIdentifier {
case "ViewEss":
print("ViewEss")
// let myTabBar = self.window?.rootViewController as! UIViewController
self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "HelloID")
case "ViewCar":
print("ViewCar")
// let myTabBar = self.window?.rootViewController as! UIViewController
self.window!.rootViewController = self.storyboard!.instantiateViewController(withIdentifier: "CarAll")
default:
print("Other Action")
self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "MainAndCar")
}
}
}
打开通知后的错误锥:
ViewEss 致命错误:在展开Optional值时意外发现nil 2017-05-08 00:00:03.291397 ade-mc-trial [2164:659918]致命错误:在解包可选值时意外发现nil (lldb)