Swift推送通知跳转页面

1.Appdelegate文件中launchOptions方法中注册通知

 func registerForPushNotifications(application: UIApplication) {

        if #available(iOS 10.0, *) {

            let center  = UNUserNotificationCenter.current()

            center.delegate = self          //设置代理

            center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in

                guard granted else { return }

                 self.getNotificationSettings()         //获取通知设置       

            }

        }else {

            

            UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))

            UIApplication.shared.registerForRemoteNotifications()            

        }

    }

func getNotificationSettings() {

        if #available(iOS 10.0, *) {

        UNUserNotificationCenter.current().getNotificationSettings { (settings) in

            guard settings.authorizationStatus == .authorized else { return }

            DispatchQueue.main.async {

                UIApplication.shared.registerForRemoteNotifications()

            }

            }

        }else{

        }        

    }

2.同时在launchOptions方法中(注册后)设置APPkill后依旧可以触发跳转功能

 //open after killed

        if #available(iOS 10.0, *) {

            

        }else{

            

            //open after killed

            if let notification = launchOptions?[.remoteNotification] as? [String: AnyObject] {

                guard notification["key"] != nil else{

                    return true

                }

                guard notification["value"] != nil  else{

                    return true

                }

                

                let key = notification["key"] as! String

                let value = notification["value"] as! String

                

                switch key {

                case "通知传过来的key值":

                    handlePushForNavigationTo(key: key值)

                    break

                case "通知传过来的key值":

                    break

                default:

                    break

                }

                

            }

3.跳转页面

func handlePushForNavigationTo(key:key值){

        

        guard key值 > 0 else { return }

        

        if let nextViewController =

            ViewController.storyboardInstance() {

            nextViewController.key值 = key值

            nextViewController.isFromNotification = true     //传递参数到跳转页面(以告知是从通知跳转过来的,便于做不同界面处理)

            let navController = UINavigationController(rootViewController: nextViewController)

        

            var rootViewController = self.window?.rootViewController

            //循环获取到最上面的viewcontroller,否则无法叠加跳转通知页面

            while ((rootViewController?.presentedViewController) != nil) {

                rootViewController = rootViewController?.presentedViewController

            }

            rootViewController?.present(

                navController, animated: true, completion: nil)

        }

        

    }

4.通知代理方法,(APP未kill,点击通知信息出发)

extension AppDelegate: UNUserNotificationCenterDelegate {

    

    @available(iOS 10.0, *)

    func userNotificationCenter(_ center: UNUserNotificationCenter,

                                didReceive response: UNNotificationResponse,

                                withCompletionHandler completionHandler: @escaping () -> Void) {

        

        let key = JSON(response.notification.request.content.userInfo)["key"].stringValue

        

        

        print(key)

        switch key {

        case "key值":

            //background

            if let groupId =

                JSON(response.notification.request.content.userInfo)["value"].int, key值 > 0{

                handlePushForNavigationTo(key:key值)

            }

            break

        default:

            

            break

            

        }

        completionHandler()

        

    }

    

    

    //Called when a notification is delivered to a foreground app.

    //Get push message when app is in foreground (show push messsage)

    @available(iOS 10.0, *)

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        

        print("userNotificationCenter -> Get push message when app is in foreground",notification.request.content.userInfo)

        print(JSON(notification.request.content.userInfo)["key"])

        let key = JSON(notification.request.content.userInfo)["key"].stringValue

        let value = JSON(notification.request.content.userInfo)["value"].numberValue

        print(key)

        print(value)

        

        //print("after clicked notification view")

        //handleGeneralPushNotification(notificationFromForegroundOrBackground: notification)

        completionHandler([.alert, .badge, .sound])

        

    }

    

    

}

猜你喜欢

转载自my.oschina.net/u/3382458/blog/1631836