我想在单击通知并关闭应用程序时推送UIViewController
以下是我在didReceiveRemoteNotification中的代码
if application.applicationState == .inactive || application.applicationState == .background {
DeeplinkHandler.handleNotification(userNotification: userNotification)
completionHandler(UIBackgroundFetchResult.newData)
}
以下是用于处理通知深层链接的代码
class func handleNotification(userNotification : UserNotification?){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
var navigationVC = UINavigationController()
if let tabBarVC = appDelegate.window?.rootViewController as? UITabBarController {
if let navVC = tabBarVC.viewControllers?[tabBarVC.selectedIndex] as? UINavigationController {
navigationVC = navVC
}
else {
tabBarVC.selectedIndex = 0
navigationVC = tabBarVC.viewControllers?[0] as! UINavigationController
}
}
// let navigationVC = appDelegate.window?.rootViewController as! UINavigationController
switch userNotification?.type ?? "" {
case DeeplinkHandler.NOTIF_TYPE_WEBVIEW:
let appWebView = AppStrings.appStoryBoard.instantiateViewController(withIdentifier: "webPageViewControllerID") as! WebPageViewController
appWebView.url = userNotification?.url ?? ""
navigationVC.pushViewController(appWebView, animated: true)
//case DeeplinkHandler.NOTIF_TYPE_PAGE_ID:
//case DeeplinkHandler.NOTIF_TYPE_FLIGHT_STATUS:
default:
let appWebView = AppStrings.appStoryBoard.instantiateViewController(withIdentifier: "notificationViewControllerID") as! NotificationViewController
//appWebView.url = userNotification?.url ?? ""
navigationVC.pushViewController(appWebView, animated: true)
}
}
但是当应用程序关闭时,单击通知时单击通知会导致崩溃。
如何处理?
在 didFinishLaunchingWithOptions
中尝试了以下代码 var notification: [AnyHashable: Any]? = (launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any])
if let notification = notification {
print("app received notification from remote\(notification)")
var userNotification : UserNotification?
if notification is [String : Any] {
userNotification = createNSaveNotification(notification)
DeeplinkHandler.handleNotification(userNotification: userNotification)
}
}
else {
print("app did not receive notification")
}
这也会在应用程序关闭时在通知点击时使应用程序崩溃
答案 0 :(得分:-2)
应用关闭时,您必须检查方法
中的通知--no-cache-dir
并检查launchOptions中的数据