代码之家  ›  专栏  ›  技术社区  ›  AtulParmar

Swift IOS:从通知打开应用程序时调用UIviewController的特定功能

  •  0
  • AtulParmar  · 技术社区  · 6 年前

    onDidReceiveNotification 在应用程序打开时使用轻触通知功能。( ViewController 是第一个视图控制器,根视图控制器)

    当应用程序打开或在后台运行时,它可以正常工作,但当应用程序未打开或在后台运行时(不在内存中),请点击此处打开应用程序 接收通知 函数不是call,

    接收通知 使用点击推送通知打开应用程序时的功能

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            NotificationCenter.default.removeObserver(self, name: .didReceiveAlert, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(ViewController.onDidReceiveNotification(_:)), name: .didReceiveAlert, object: nil)
        }
    
    @objc func onDidReceiveNotification(_ notification:Notification) {
            print("Received Notification")
        }
    }
    

    AppDelegate.swift批准

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {            
        NotificationCenter.default.post(name: .didReceiveAlert, object: nil)       
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   AtulParmar    6 年前

    当应用程序打开或在后台时调用 NotificationCenter.defalut.post 当应用程序终止/关闭并使用通知打开时,获取实例并通过菜单调用 onDidReceiveAlert

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
        if application.applicationState == .background || application.applicationState == .active {
            NotificationCenter.default.post(name: .didReceiveAlert, object: nil)
        }else {
            let nav = window?.rootViewController as! UINavigationController
            let mainVC = nav.viewControllers.first as! ViewController
            mainVC.onDidReceiveAlert(nil)
        }
    }
    

    或者直接得到 ViewController 实例来自 UINavigationController 视图控制器 (不需要通知观察员)

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
        let nav = window?.rootViewController as! UINavigationController
        let mainVC = nav.viewControllers.first as! ViewController
        mainVC.onDidReceiveAlert(nil)       
    }
    

    @objc func onDidReceiveAlert(_ notification:Notification? = nil) {
        print("Received Notification")
    }
    
        2
  •  0
  •   Akhilrajtr    6 年前

    尝试下面的代码

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any] {
             //call your notification method from here
             NotificationCenter.default.post(name: .didReceiveAlert, object: nil)  
        }
    }
    

    希望你只需要打电话给 didReceiveAlert 任何情况下的通知

        3
  •  0
  •   Yogesh Tandel    6 年前

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        // Print full message.
        print(userInfo)
        pushToPage(data: userInfo)
    }
    
    func pushToPage(data:[AnyHashable : Any]){
        print("Push notification received: \(data)")
        if let aps = data["aps"] as? NSDictionary {
            print(aps)
        }
    }
    
        4
  •  0
  •   Nilesh R Patel    6 年前

    当应用程序处于非活动状态并且用户点击通知时。在这种情况下,只有一个选项来管理通知。

    试试这个

    前任 在中创建变量

    var isNotificationTap : Bool = false
    

    在那张支票之后 launchOptions 如果 可用然后设置标志

    if launchOptions != nil {
        if let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String : AnyObject] {
            if let dic = userInfo["body"] as? [String : AnyObject] {
                if let pushType = dic["push_type"] as? String {
                    //Set Flag or any key in global file to identify that you tap on notification
                    self.isNotifcaionTap = true
                }
            }
        }
    }
    

    然后简单地在ViewController中检查一个标志。

    class ViewController : UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            if self.isNotificationTap {
    
                print("Received Notification")
                //You can work with your notificaiton in inActive state
            }
        }
    
    }
    
        5
  •  -1
  •   Arul Murugan Sivapoosam    6 年前

    if let rootController = UIApplication.shared.keyWindow?.rootViewController{
            let controllerInstance = rootController.navigationController?.viewControllers[yourControllerIndex] as? ViewController
            controllerInstance.onDidReceiveNotification()
     }
    
    推荐文章