推送通知 – 在SWIFT中收到推送通知后控制哪个视图控制器加载
一旦我收到推送通知并滑动打开它,它只是打开我的应用程序,而不是我想要的VC。
所以我的问题是如何加载我想要的VC?我知道如果应用程序是开放的,我会将VC移动到didReceiveRemoteNotification中的另一个,但如果该应用程序未打开,该怎么办?或者是否在后台模式? 另外我有两个不同的推送通知,所以我需要它移动一个两个不同的VC。我如何知道不同的推动不同的区别? 谢谢。
更新为Swift 3.0
就像有人说,你想在applicationDidLaunchWithOptions中注册远程通知: func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let pushSettings = UIUserNotificationSettings(types: [.alert,.badge,.sound],categories: nil) UIApplication.shared.registerUserNotificationSettings(pushSettings) UIApplication.shared.registerForRemoteNotifications() } 当您从lockScreen / Background返回时,无法知道您将在哪个viewController中。我所做的是从appDelegate发送通知。当您收到remoteNotification时,appDelegate中的didReceiveRemoteNotification被调用。 func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { let notif = JSON(userInfo) // SwiftyJSON required 根据您的通知所包含的内容,您应该首先确保它不是零,然后调用将被捕获该通知的viewController捕获的通知。可能看起来像这样,只是把它作为一个例子: if notif["callback"]["type"] != nil{ NotificationCenter.default.post(name: Notification.Name(rawValue: "myNotif"),object: nil) // This is where you read your JSON to know what kind of notification you received,for example : } 例如,如果您收到消息通知,并且您没有登录,因为令牌已过期,则通知将永远不会捕获在视图控制器中,因为它将永远不会被监视。 现在,您在视图控制器中捕获通知的部分。在viewWillAppear: override func viewWillAppear(_ animated: Bool) { NotificationCenter.default.addObserver(self,selector: #selector(self.catchIt),name: NSNotification.Name(rawValue: "myNotif"),object: nil) } 现在你添加了这个观察者,每次在这个控制器中调用一个通知时,函数catchIt也会被调用。您将必须在每个要执行特定操作的视图控制器中实现它。 func catchIt(_ userInfo: Notification){ if userInfo.userInfo?["userInfo"] != nil{ let prefs: UserDefaults = UserDefaults.standard prefs.removeObject(forKey: "startUpNotif") prefs.synchronize() let storyboard = UIStoryboard(name: "Main",bundle: nil) let vc: RedirectAppInactiveVC = storyboard.instantiateViewController(withIdentifier: "RedirectAppInactiveVC") as! RedirectAppInactiveVC self.navigationController?.pushViewController(vc,animated: true) } else{ let storyboard = UIStoryboard(name: "Main",bundle: nil) let vc: RedirectAppActiveVC = storyboard.instantiateViewController(withIdentifier: "RedirectAppActiveVC") as! RedirectAppActiveVC self.navigationController?.pushViewController(vc,animated: true) } } 离开视图控制器时不要忘记取消订阅通知,否则viewController(如果仍然在堆栈中)将捕获通知并执行它(您可能希望这样做,但是知道您将要进行的更安全) )。所以我建议在viewWillDisappear中取消订阅: override func viewWillDisappear(_ animated: Bool) { NotificationCenter.default.removeObserver(self) } 这样做,你将加载你想要的viewController。现在我们还没有对待所有的案件。如果你还没有打开你的应用程序怎么办显然没有UIViewController被加载,并且它们都不能捕获通知。你想知道你是否在doFinishLaunchingWithOptions中收到通知:在appDelegate中。我做的是: let prefs: UserDefaults = UserDefaults.standard if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary { prefs.set(remoteNotification as! [AnyHashable: Any],forKey: "startUpNotif") prefs.synchronize() } 现在,您已经设置了使用远程通知启动应用程序的首选项。在应用程序中应首先加载的控制器中,我建议在viewDidAppear中执行以下操作: override func viewDidAppear(animated: Bool) { let prefs:UserDefaults = UserDefaults.standard if prefs.value(forKey: "startUpNotif") != nil{ let userInfo: [AnyHashable: Any] = ["inactive": "inactive"] NotificationCenter.default.post(name: Notification.Name(rawValue: "myNotif"),object: nil,userInfo: userInfo as [AnyHashable: Any]) } 希望它有帮助。我还制作了一个github仓库来说明本地通知:Local Notifications Observer Pattern(类似于远程通知)。可以使用根视图Controller Local Notifications Root Pattern来实现类似的逻辑,我个人认为它将取决于您要实现的内容。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |