以下是我收到的代码
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
var userNotification : UserNotification?
if userInfo is [String : Any] {
userNotification = createNSaveNotification(userInfo)
}
DeeplinkHandler.handleNotification(userNotification: userNotification)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
//Called when a notification is delivered to a foreground app.
let userInfo = notification.request.content.userInfo as? NSDictionary
print("\(userInfo)")
var userNotification : UserNotification?
if userInfo is [String : Any] {
userNotification = createNSaveNotification(userInfo as! [AnyHashable : Any])
}
DeeplinkHandler.handleNotification(userNotification: userNotification)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// Called to let your app know which action was selected by the user for a given notification.
let userInfo = response.notification.request.content.userInfo as? NSDictionary
print("\(userInfo)")
var userNotification : UserNotification?
if userInfo is [String : Any] {
userNotification = createNSaveNotification(userInfo as! [AnyHashable : Any])
}
DeeplinkHandler.handleNotification(userNotification: userNotification)
}
以下是 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")
}
以下是处理深度链接的代码
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)
}
}
现在它在通常情况下工作正常,但是当应用程序关闭并且用户单击通知时崩溃,这是因为 RootViewController 在这种情况下为 nil 如何在单击关闭应用程序的通知时先推送 rootviewcontroller,然后再深度链接相关的视图控制器
更新:还发现如果我在代码下面评论它没有崩溃但它也没有按照深层链接重定向,它工作正常。
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
}
}
这可能是因为我在 UITabBarController 的 viewDidLoad 中设置了 UITabBar 视图控制器