Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 收到推送通知时加载ViewController_Swift - Fatal编程技术网

Swift 收到推送通知时加载ViewController

Swift 收到推送通知时加载ViewController,swift,Swift,当用户收到推送通知并点击该通知时,他/她将被带到我的应用程序中,我希望某个视图控制器出现在该应用程序中。因此,我使用通知中心 我的问题是,我需要在哪里执行视图控制器的加载,以便在用户进入应用程序时在导航堆栈上显示和推送视图控制器 func processReceivedRemoteNotification(userInfo:[NSObject:AnyObject]) { let notification = userInfo as! Dictionary<String, AnyO

当用户收到推送通知并点击该通知时,他/她将被带到我的应用程序中,我希望某个视图控制器出现在该应用程序中。因此,我使用通知中心

我的问题是,我需要在哪里执行视图控制器的加载,以便在用户进入应用程序时在导航堆栈上显示和推送视图控制器

func processReceivedRemoteNotification(userInfo:[NSObject:AnyObject]) {

    let notification = userInfo as! Dictionary<String, AnyObject>

    let json = JSON(notification)

    // Get information from payload
    let dispatchType:String = json["dispatch"]["dispatchType"].stringValue

    switch dispatchType {
    case "alert":
        self.notificationCenter.postNotificationName("ALERT_RECEIVED", object: nil, userInfo: userInfo as [NSObject:AnyObject])
        break
    default:
        break
    }
}
func processReceivedRemoteNotification(用户信息:[NSObject:AnyObject]){
让notification=userInfo as!Dictionary
让json=json(通知)
//从有效载荷获取信息
让dispatchType:String=json[“dispatch”][“dispatchType”]。stringValue
交换机调度类型{
案例“警报”:
self.notificationCenter.postNotificationName(“收到警报”,对象:nil,userInfo:userInfo作为[NSObject:AnyObject])
打破
违约:
打破
}
}
查看要加载的控制器

class AlertViewController: UIViewController {

    let notificationCenter: NSNotificationCenter = NSNotificationCenter.defaultCenter()

    override func viewWillAppear(animated: Bool) {
        self.notificationCenter.addObserver(self, selector: "alertMessageReceived:", name: "ALERT_RECEIVED", object: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func alertMessageReceived(notification: NSNotification) {
        let userInfo = notification.userInfo as! Dictionary<String, AnyObject>
        print(userInfo)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc1: AlertViewController = storyboard.instantiateViewControllerWithIdentifier("example1") as! AlertViewController
        self.navigationController?.pushViewController(vc1, animated: true)
    }
类AlertViewController:UIViewController{
让notificationCenter:NSNotificationCenter=NSNotificationCenter.defaultCenter()
覆盖功能视图将出现(动画:Bool){
self.notificationCenter.addObserver(self,选择器:“alertMessageReceived:”,名称:“ALERT_RECEIVED”,对象:nil)
}
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
收到func AlertMessage(通知:NSNotification){
让userInfo=notification.userInfo作为!字典
打印(用户信息)
let storyboard=UIStoryboard(名称:“Main”,捆绑包:nil)
将vc1:AlertViewController=storyboard.InstanceEviewController标识符(“示例1”)设置为!AlertViewController
self.navigationController?.pushViewController(vc1,动画:true)
}

我不知道你的应用程序架构,但从给定的上下文中我可以看到你有一个
navigationController
。在这种情况下,你不应该添加为观察者
AlertViewController
。而是将此代码移动到另一个视图控制器,该视图控制器已推送到
navigationController
。另一个选项是类
UINavigationController
并观察
“收到警报”
notification在其中。

processReceivedRemoteNotification位于AppDelegate中。因此,我会将代码直接加载到上面方法中的switch语句中?如果您在AppDelegate中有对导航控制器的引用,您可以在那里执行。您可以向我显示代码,其中导航控制器是cre吗ated?好的,你的根视图控制器是什么?我的根视图是启动时加载的mapview控制器,它是嵌入导航控制器中的应用程序的主视图