我正在开发一个消息传递应用程序,但我在实现推送通知行为方面遇到了麻烦。
我想要的是,当用户点击通知时,该组的会话将打开。但……
我认为你应该和NotificationCenter一起去做。
当用户点击通知时,您应该在didReceive中发布NotificationCenter,如下所示:
NotificationCenter.default.post(name: .onMessageRecieved, object: notificationObject)
根据你所在的视图控制器,你应该在viewDidLoad()函数中添加一个观察者,这样它就能理解收到的消息是这样的:
override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(MyViewController.onMessageRecieved(notification:)), name: .onMessageRecieved, object: nil) }
你应该在视图控制器中有一个func(onMessageRecieved)来执行这样的导航:
func onMessageRecieved(notification: NSNotification){ // open the conversation let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController vc.contactName = name as String self.navigationController?.pushViewController(vc, animated: true) }
这样您就可以从视图控制器进行导航,因此导航也会在那里。
无法将“SWRevealViewController”类型的值强制转换为UINavigationController
这个问题是因为“SWRevealViewController”不是UINavigationController的子类。所以你不能这样投。
要解决聊天ViewController问题,您应该像这样修改代码。
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController let nav = vc.navigationController vc.contactName = name as String let rootViewController = self.window?rootViewController as! UIViewController rootViewController.presentViewController(nav, animated: true, completion: nil)
但聊天VC将处于演示者模式。你应该处理解雇。
你试试这个,让我知道它是否有效。