Ios 如何重定向到推送通知的特定页面?

Ios 如何重定向到推送通知的特定页面?,ios,apple-push-notifications,swift4,Ios,Apple Push Notifications,Swift4,我正在处理ios推送通知,需要在点击时将其重定向到特定页面。 如何实现这一功能 若要在收到通知时重定向到特定页面,必须首先处理收到的推送通知 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchRe

我正在处理ios推送通知,需要在点击时将其重定向到特定页面。
如何实现这一功能

若要在收到通知时重定向到特定页面,必须首先处理收到的推送通知

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


        print("userInfo:->  \(userInfo)")
        let redirect_flag = userInfo["redirect_flag"]as! String


        if application.applicationState == .inactive {

            //MARK: - code here to redirect when app is not in background or inactive


            if (UserDefaults.standard.value(forKey: KEY_IS_LOGIN) != nil)
            {

                switch redirect_flag {
                case "1":

                    print("redirect controller")

                case "2":

                      print("redirect controller")

                case "3":

                      print("redirect controller")

                default:
                    break
                }

            }



        }else {

             //MARK: - code here to redirect when app is in background or active mode

            switch redirect_flag {
            case "1":

                print("redirect controller")

            case "2":

                print("redirect controller")

            case "3":

                print("redirect controller")

            default:
                break
            }
        }

    }
要处理收到的推送通知,请执行以下步骤:

  • didfishlaunchwithoptions中
    将消息传递委托设置为self
  • 注册远程通知。这将显示首次运行时的权限对话框
  • 您需要处理iOS 10及之前的通知注册
  • 实现didReceiveEmotentification开始接收消息,在这里您可以处理接收到的数据,解析数据并重定向。。。当为运行低于10的iOS的设备发送通知时,将调用此命令
  • 对于iOS 10及更高版本,您可以遵循
    UnuseNotificationCenterDelegate
    ,并实现
    didReceive
    方法。就是这样
  • 示例:

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    
            // [START set_messaging_delegate]
            Messaging.messaging().delegate = self
            // [END set_messaging_delegate]
    
            // Register for remote notifications. This shows a permission dialog on first run, to
            // show the dialog at a more appropriate time move this registration accordingly.
            // [START register_for_notifications]
            if #available(iOS 10.0, *) {
                // For iOS 10 display notification (sent via APNS)
                UNUserNotificationCenter.current().delegate = self
    
                let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
                UNUserNotificationCenter.current().requestAuthorization(
                    options: authOptions,
                    completionHandler: {_, _ in })
            } else {
                let settings: UIUserNotificationSettings =
                    UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
                application.registerUserNotificationSettings(settings)
            }
    
            application.registerForRemoteNotifications()
            return true
        }
    
    处理收到的消息:

      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                         fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
            // If you are receiving a notification message while your app is in the background,
            // this callback will not be fired till the user taps on the notification launching the application.
            // TODO: Handle data of notification
    
            // Print full message.
            print(userInfo)
    
            completionHandler(UIBackgroundFetchResult.newData)
        }
    
    处理收到的iOS 10及更高版本的通知:

    @available(iOS 10, *)
    extension AppDelegate : UNUserNotificationCenterDelegate {
    
        // Receive displayed notifications for iOS 10 devices.
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    willPresent notification: UNNotification,
                                    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            let userInfo = notification.request.content.userInfo
    
            if let messageID = userInfo[gcmMessageIDKey] {
                print("Message ID: \(messageID)")
            }
    
            // Print full message.
            print(userInfo)
    
            // Change this to your preferred presentation option
            completionHandler([.alert, .badge, .sound])
        }
    
        //When clicked
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    didReceive response: UNNotificationResponse,
                                    withCompletionHandler completionHandler: @escaping () -> Void) {
            let userInfo = response.notification.request.content.userInfo
            // Print message ID.
            if let messageID = userInfo[gcmMessageIDKey] {
                print("Message ID: \(messageID)")
            }
    
            // Print full message.
            print(userInfo)
            //You can here parse it, and redirect.
            completionHandler()
        }
    }
    

    您可以遵循UnuseNotificationCenterDelegate协议并实现didReceive方法,该方法将在单击通知时调用。请给出一个实现示例。