How to make your push notification Open a certain view controller?

后端 未结 4 746
星月不相逢
星月不相逢 2020-12-09 00:43

I looked on SO but I wasn\'t able to find any question that discusses when you receive a push notification how can you then open a specific view controller. For example if y

4条回答
  •  一向
    一向 (楼主)
    2020-12-09 01:04

    This method is called when user tap on the notification. implement in appdelegate

    var navigationC: UINavigationController?
    
     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
        if USERDEFAULT.value(forKey: "isLogin") != nil  {
            let tabbarvc = MainStoryboard.instantiateViewController(withIdentifier: "TabBarVC") as! TabBarVC
            self.navigationC = UINavigationController(rootViewController: tabbarvc)
        }else {
            let loginvc = MainStoryboard.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
            self.navigationC = UINavigationController(rootViewController: loginvc)
        }
    
        self.navigationC?.setNavigationBarHidden(true, animated: false)
        self.window?.clipsToBounds = true
        self.window?.rootViewController = navigationC
        self.window?.makeKeyAndVisible()
    
        return true
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        print("-------------\nUser info in notification -> \(userInfo)\n-------------")
        let dict = userInfo["aps"] as? NSDictionary ?? [:]
        if USERDEFAULT.value(forKey:"isLogin") != nil{
            let type = dict["type"] as? Int ?? 0
            switch type {
            case 0:
                break
            case 1:
                for vc in self.navigationC!.viewControllers {
                    if vc is TabBarVc {
                        let  exam = dict["exam"] as? String ?? ""
                        if exam == ""{
                            let TestVC = MainStoryboard.instantiateViewController(withIdentifier: "TestVC") as! TestVC
                            TestVC.chapterId = dict["chapter_id"] as? String ?? ""
                            TestVC.strSubTitle = dict["chapter"] as? String ?? ""
                            self.navigationC?.isNavigationBarHidden = true
                            self.navigationC?.pushViewController(TestVC, animated: true)
                        }else if exam != ""{
                            let ExamTestVC = MainStoryboard.instantiateViewController(withIdentifier: "ExamTestVC") as! ExamTestVC
                            ExamTestVC.examId = dict["exam_id"] as? String ?? ""
                            ExamTestVC.strSubTitle = dict["exam"] as? String ?? ""
                            self.navigationC?.isNavigationBarHidden = true
                            self.navigationC?.pushViewController(ExamTestVC, animated: true)
                        }
                        return;
                    }
                }
                break
    
            case 2:
                for vc in self.navigationC!.viewControllers {
                    if vc is TabBarVc {
                        let VideoListVC = MainStoryboard.instantiateViewController(withIdentifier: "VideoListVC") as! VideoListVC
                        VideoListVC.chapterId = dict["chapter_id"] as? String ?? ""
                        VideoListVC.video_id = dict["video_id"] as? String ?? ""
                        VideoListVC.strSubTitle = dict["chapter"] as? String ?? ""
                        VideoListVC.questionsCount = dict["question_count"] as? Int ?? 0
                        VideoListVC.testCount = dict["test_count"] as? Int ?? 0
                        self.navigationC?.isNavigationBarHidden = true
                        self.navigationC?.pushViewController(VideoListVC, animated: true)
                        return;
                    }
                }
                break
    
            case 3:
    
                break
    
            default: break
    
            }
        }else{
            let loginVC = SignupiPadStoryboard.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
            SignupStoryboard = SignupiPadStoryboard
            self.navigationC = UINavigationController(rootViewController: loginVC)
        }
    
    }
    

    OR you can direct open view controller like whatsapp chat..etc.

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        let userInfo = notification.request.content.userInfo
        let dictAps = userInfo["aps"] as! NSDictionary
        print(dictAps)
        let notification_type = dictAps["notification_type"] as? Int ?? 0
        if notification_type == 6{
            if isChatActive == false{
                completionHandler([.alert, .badge, .sound])
            }else{
                if USERDEFAULT.value(forKey:"isLogin") != nil{
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ReloadChatMessages"), object: nil, userInfo: (dictAps as! [AnyHashable : Any]))
                }else{
                    completionHandler([.alert, .badge, .sound])
                }
            }
        }else{
            completionHandler([.alert, .badge, .sound])
        }
    
    }
    

提交回复
热议问题