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

后端 未结 4 736
星月不相逢
星月不相逢 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])
        }
    
    }
    
    0 讨论(0)
  • 2020-12-09 01:10
    //This method is called when user tap on the notification
    
          func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
               print("user clicked on the notification")
               let userInfo = response.notification.request.content.userInfo
    
               print(userInfo)
    
               //check your response and navigate to specific view controller
               moveToNextViewController()
          }
    
         func moveToNextViewController() {
        //Add code for present or push view controller
               let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier:"ViewController") as! ViewController                    
              self.navigationController?.pushViewController(vc, animated: true)
        }
    
    0 讨论(0)
  • 2020-12-09 01:19

    You can detect if the app opened from the notification with this code in app delegate. You will need to set the initial view controller when the application state is UIApplicationStateInactive before the app has become active. You can perform any logic there to decide which view controller should be opened and what content should be shown in that view controller.

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    
        if(application.applicationState == UIApplicationStateActive) {
    
            //app is currently active, can update badges count here
    
        } else if(application.applicationState == UIApplicationStateBackground){
    
            //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
    
        } else if(application.applicationState == UIApplicationStateInactive){
    
            //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
    
            self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    
            UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
    
            self.window.rootViewController = viewController;
            [self.window makeKeyAndVisible];
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-09 01:29

    Here is the Swift 3 Version with switch/case instead of if/else

        open func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        switch application.applicationState {
        case .active:
            print("do stuff in case App is active")
        case .background:
            print("do stuff in case App is in background")
        case .inactive:
            print("do stuff in case App is inactive")
        }
    }
    
    0 讨论(0)
提交回复
热议问题