应用程序关闭时的IOS句柄推送通知

应用程序关闭时的IOS句柄推送通知,ios,swift,push-notification,apple-push-notifications,nsnotificationcenter,Ios,Swift,Push Notification,Apple Push Notifications,Nsnotificationcenter,我想在单击通知并关闭应用程序时推送UIViewController 以下是我在DidReceiveMotonification中的代码 if application.applicationState == .inactive || application.applicationState == .background { DeeplinkHandler.handleNotification(userNotification: userNotification) c

我想在单击通知并关闭应用程序时推送UIViewController

以下是我在DidReceiveMotonification中的代码

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)

    }
}
但通知是,当应用程序关闭时,单击通知会导致崩溃

如何处理

尝试在didfishlaunchingwithoptions中执行以下代码

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

这也是在通知时崩溃应用程序关闭时单击应用程序

当应用程序关闭时,您必须在方法中检查通知

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions;

-(BOOL)应用程序:(UIApplication*)应用程序
didFinishLaunchingWithOptions:(NSDictionary*)启动选项;

并检查启动选项中的数据

建议您使用并实施“导致崩溃”的方法“什么崩溃?显示崩溃日志。显示相应的代码。由于应用程序已关闭并再次启动,因此它没有显示任何日志是的,崩溃日志仍然存在。另外,
如果application.applicationState==.inactive | | application.applicationState==.inactive
是胡说八道的。问题很简单。”