问题
I have an ios 11 app that uses local push notifications for remainders at a given time. The local notifications work and when swiped they open up the main view of my app. My app's main view is a uitableviewcontroller. The reminders are rows in the table view.
If you click on a reminder it open's a new view controller by self.presentview... The new view pops up over the table view. I am not using storyboards or xib files and doing it programmatically.
How do I pass the reminder id to the push notification and back to the app and then have that reminder id open the second view controller? The second view controller has more details about the reminder.
回答1:
While scheduling local notification you can set the required details in userInfo
let notification = UILocalNotification()
notification.fireDate = date
notification.alertBody = "Alert!"
notification.alertAction = "open"
notification.hasAction = true
notification.userInfo = ["reminderID": "222" ]
You will get the userInfo in didReceiveRemoteNotification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("didReceiveRemoteNotificationfetchCompletionHandler \(userInfo)")
/// HANDLE NAVIGATION HERE
}
回答2:
Building upon the answer of anuraj. set the required details in userInfo
Then handle the navigation using the following method (in appDelegate.swift
)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("didReceiveRemoteNotificationfetchCompletionHandler \(userInfo)")
let id = userInfo["reminderID"] as! String
/// HANDLE NAVIGATION HERE
if let tableVC = self.window?.rootViewController as? yourTableViewControllerClass {
let reminderDetailsVC = yourReminderDetailsVC()
reminderDetailsVC.reminderID = id
tableVC.present(reminderDetailsVC, animated: true)
}
}
Let me know if you need any help
来源:https://stackoverflow.com/questions/51626479/open-a-view-controller-from-a-local-notification-in-ios-11