Open a view controller from a local notification in ios 11

寵の児 提交于 2019-12-13 03:56:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!