我的应用程序类型A和类型B中有两种类型的推送通知,并且根据每种类型,我希望将用户导航到不同的视图控件。现在,如果用户单击该应用程序,则该应用程序仅显示homeVC 通知,我可以将值从通知对象传递给 查看控件。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
userInfo["Type"] as? String == "TypeA" {
showNotificationA()
} else {
showNotificationB()
}
completionHandler(UIBackgroundFetchResult.newData)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// How to identify which notification the user clicked to navigate to the right view?
if UIApplication.shared.applicationState == .inactive {
}
completionHandler()
}
答案 0 :(得分:0)
基本上,推送通知的处理大致由以下步骤组成:
这是在AppDelegate类中完成的,类似于:
UNUserNotificationCenter
.current()
.requestAuthorization(options: [.alert, .badge]) { (granted, error) in
UNUserNotificationCenter.current().delegate = MyCustomDelegateEntity
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
,该委托会接收传入的通知在您的特定情况下,您应该以类似的方式从NotificationDelegate实例化所需的视图控制器
let rootVC = MyViewController.instantiateFrom(storyboard: "StoryboardName")
let navigationController = UINavigationController(rootViewController: rootVC)
(UIApplication.shared.delegate as? AppDelegate)?.window?.rootViewController = navigationController
ViewController.instantiateFrom(storyboard :)内容类似于:
return UIStoryboard(name: storyboard, bundle: nil).instantiateViewController(withIdentifier: String(describing: self)) as! MyViewController
答案 1 :(得分:-1)
您可以将所有通知重定向到一个控制器,然后在此basview控制器中检查其来自哪种通知类型并进行相应导航。
您可以传递来自通知有效负载的数据并进行相应处理