Swift push notification jump page

1. Register notifications in the launchOptions method in the Appdelegate file

 func registerForPushNotifications(application: UIApplication) {

        if #available(iOS 10.0, *) {

            let center  = UNUserNotificationCenter.current()

            center.delegate = self //Set the delegate

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

                guard granted else { return }

                 self.getNotificationSettings() //Get notification settings       

            }

        }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. At the same time, the jump function can still be triggered after setting APPkill in the launchOptions method (after registration).

 //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 "Notify passed key value":

                    handlePushForNavigationTo(key: key值)

                    break

                case "Notify passed key value":

                    break

                default:

                    break

                }

                

            }

 

3. Jump page

func handlePushForNavigationTo(key:key值){

        

        guard key值 > 0 else { return }

        

        if let nextViewController =

            ViewController.storyboardInstance() {

            nextViewController.key value = key value

            nextViewController.isFromNotification = true //Pass parameters to the jump page (to inform that it is jumped from the notification, which is convenient for different interface processing)

            let navController = UINavigationController(rootViewController: nextViewController)

        

            var rootViewController = self.window? .rootViewController

            //loop to get the top viewcontroller, otherwise the jump notification page cannot be superimposed

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

                rootViewController = rootViewController? .presentedViewController

            }

            rootViewController?.present(

                navController, animated: true, completion: nil)

        }

        

    }

4. Notification agent method, (APP is not killed, click the notification information to start)

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])

        

    }

    

    

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325434512&siteId=291194637