gpt4 book ai didi

swift - 收到推送通知时加载 ViewController

转载 作者:行者123 更新时间:2023-11-30 13:36:48 26 4
gpt4 key购买 nike

当用户收到推送通知并点击通知时,他/她将被带入我的应用程序,我希望在其中出现某个 View Controller 。因此我使用通知中心。

我的问题是,我需要在哪里执行 View Controller 的加载,以便在用户进入应用程序时将其显示并推送到导航堆栈上?

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
}
}

要加载的 View Controller

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)
}

最佳答案

我不知道您的应用架构,但从给定的上下文中我可以看到您有一个 navigationController。在这种情况下,您不应将 AlertViewController 添加为观察者。相反,将此代码移至另一个 View Controller ,该 View Controller 已推送至 navigationController。另一种选择是子类化 UINavigationController 并观察其中的 "ALERT_RECEIVED" 通知。

关于swift - 收到推送通知时加载 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35973483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com