Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 处理用户在单击通知时的去向?_Ios_Swift_Apple Push Notifications_Swift5 - Fatal编程技术网

Ios 处理用户在单击通知时的去向?

Ios 处理用户在单击通知时的去向?,ios,swift,apple-push-notifications,swift5,Ios,Swift,Apple Push Notifications,Swift5,我有几种不同类型的通知,当单击通知时,所有这些通知都会将用户带到不同的视图控制器 应该如何处理(顺便说一下,我使用的是Swift 5)?从我的研究中,我发现人们倾向于在AppDelegate的didReceive函数中提供一个新的视图控制器,但是对于几个不同的视图控制器,在AppDelegate中执行所有逻辑似乎都是错误的。这真的是正确的做法吗 此外,我正在使用Firebase从后端向设备发送消息。我有一个单独的类,FirebaseUtils,我在其中处理传递的数据的所有逻辑。从这里显示视图控制

我有几种不同类型的通知,当单击通知时,所有这些通知都会将用户带到不同的视图控制器

应该如何处理(顺便说一下,我使用的是Swift 5)?从我的研究中,我发现人们倾向于在
AppDelegate
didReceive
函数中提供一个新的视图控制器,但是对于几个不同的视图控制器,在
AppDelegate
中执行所有逻辑似乎都是错误的。这真的是正确的做法吗


此外,我正在使用Firebase从后端向设备发送消息。我有一个单独的类,
FirebaseUtils
,我在其中处理传递的数据的所有逻辑。从这里显示视图控制器是否更好?如果是这样的话,没有根视图控制器,我该怎么做呢?

我通常会按照以下思路设置一些东西(未经测试):

  • 为可能处理通知的事物创建一个
    NotificationHandler
    协议
  • 在AppDelegate中创建一个
    notificationHandlers
    变量,并用可能需要处理通知的内容填充该变量
didReceive
中,循环处理程序,询问每个处理程序是否可以处理通知,如果可以,则告诉它这样做

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    guard let handler = notificationHandlers.first(where: 
        { $0.canHandle(notification: userInfo) }) {
    else {
        return
    }

    handler.handle(notification: userInfo, completionHandler: completionHandler)
}

这种方法将逻辑排除在AppDelegate之外,这是正确的,并防止其他类型在AppDelegate内部四处窥探,这也是正确的。

您希望这样做吗

struct NotificationPresenter {
   func present(notification: [AnyHashable: Any], from viewController: UIViewController) {
      let notificationViewController: UIViewController

      // decide what type of view controller to show and set it up

      viewController.present(notificationViewController, animated: true, completion: nil)
   }
}

谢谢但我仍然不确定如何从NotificationHandler本身呈现视图控制器。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    guard let handler = notificationHandlers.first(where: 
        { $0.canHandle(notification: userInfo) }) {
    else {
        return
    }

    handler.handle(notification: userInfo, completionHandler: completionHandler)
}
struct NotificationPresenter {
   func present(notification: [AnyHashable: Any], from viewController: UIViewController) {
      let notificationViewController: UIViewController

      // decide what type of view controller to show and set it up

      viewController.present(notificationViewController, animated: true, completion: nil)
   }
}
extension UIViewController {
   static func topViewController(_ parentViewController: UIViewController? = nil) -> UIViewController {
      guard let parentViewController = parentViewController else {
         return topController(UIApplication.shared.keyWindow!.rootViewController!)
      }

      return parentViewController.presentedViewController ?? parentViewController
   }
}

let notificationPresenter = NotificationPresenter()

func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable: Any], 
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
   notificationPresenter.present(userInfo, from: UIViewController.topViewController())
}