[英]iOS push notification click causes app to crash
I am having an issue with my push notification click. 我的推送通知点击有问题。 Everytime user clicks on the notifications, the app will crash instead of redirecting user to the specified page.
每次用户单击通知时,应用程序将崩溃,而不是将用户重定向到指定页面。
This part of the code is causing an error "Could not cast value of type 'appname.LaunchScreenController' to 'UINavigationController'" : 这部分代码导致错误“无法将类型'appname.LaunchScreenController'的值强制转换为'UINavigationController'” :
let rootViewController = self.window!.rootViewController as! UINavigationController
And this code will cause fatal error: unexpectedly found nil while unwrapping an Optional value : 并且此代码将导致致命错误:解开Optional值时意外发现nil :
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
//receive the notifications
NotificationCenter.default.post(name: Notification.Name(rawValue: "MyNotificationType"), object: nil, userInfo: userInfo)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewsController") as! NewsViewController
let rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.pushViewController(vc, animated:true)
}
Thanks in advance 提前致谢
RootViewController is subclass of UIViewController
not a UINavigationController
You have to handle your null
values RootViewController是
UIViewController
子类,而不是UINavigationController
您必须处理null
值
change to 改成
let rootViewController = self.window!.rootViewController
I change the code to this and it works just fine now 我将代码更改为此并且现在可以正常工作
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
NotificationCenter.default.post(name: Notification.Name(rawValue: "MyNotificationType"), object: nil, userInfo: userInfo)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewsController") as! NewsViewController
let nav = UINavigationController()
nav.pushViewController(vc, animated: true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.