点击用户通知即可转到特定视图

时间:2018-10-18 11:37:05

标签: ios swift push-notification uinavigationcontroller uinavigationbar

我已经在项目中实现了推送通知,单击了推送通知(处于非活动,后台和前台模式),我将用户从appdelegate定向到特定的Viewcontroller,那里有通知列表,这是代码: / p>

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
 if let messageID = userInfo[gcmMessageIDKey] {
                    print("Message ID: \(messageID)")
                }
   let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                        let initialViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "VANotificationTableViewController") as! VANotificationViewController

                        self.window = UIWindow(frame: UIScreen.main.bounds)
                        self.window?.rootViewController = initialViewController
                        self.window?.makeKeyAndVisible()
                //showPushNotification(message: response.notification.request.content.body)

                completionHandler()
            }

基本上,我将通知视图控制器作为rootViewController 在通知视图控制器中单击“后退”按钮时,它应该显示“仪表板”视图控制器,但仪表板视图控制器即将在其顶部没有通知栏。 我尝试过的事情

  • Self.dismiss
  • 尝试过segue
  • 从通知视图控制器上的后退操作中将仪表板控制器设置为根视图控制器

    有没有办法让我可以将视图从AppDelegate导航到Notification ViewController,从而使代码工作正常?

1 个答案:

答案 0 :(得分:0)

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }
    let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "VANotificationTableViewController") as! VANotificationViewController
    let nav  = UINavigationController.init(rootViewController: initialViewController)
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = nav
    self.window?.makeKeyAndVisible()
    //showPushNotification(message: response.notification.request.content.body)
    completionHandler()}