swift – 从远程通知打开ViewController
当我的应用程序捕获远程通知时,我尝试打开一个特定的ViewController.
让我展示我的项目架构. 当我收到通知时,我想打开一个“SimplePostViewController”,所以这是我的appDelegate: var window: UIWindow? var navigationVC: UINavigationController? func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert,UIUserNotificationType.Badge,UIUserNotificationType.Sound] let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes,categories: nil) let storyboard = UIStoryboard(name: "Main",bundle: nil) self.navigationVC = storyboard.instantiateViewControllerWithIdentifier("LastestPostsNavigationController") as? UINavigationController application.registerUserNotificationSettings(pushNotificationSettings) application.registerForRemoteNotifications() return true } func application(application: UIApplication,didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { if let postId = userInfo["postId"] as? String { print(postId) let api = EVWordPressAPI(wordpressOauth2Settings: WordPress.wordpressOauth2Settings,site: WordPress.siteName) api.postById(postId) { post in if (post != nil) { self.navigationVC!.pushViewController(SimplePostViewController(),animated: true) } else { print("An error occurred") } } } } 我在应用程序启动时保存了我的UINavigationViewController,并在收到通知时尝试推送新的SimplePostViewController.但什么都没发生. 我还使用了“什么是新的”视图添加执行我的segue但也没有发生任何事情. 方案: for child in (self.rootViewController?.childViewControllers)! { if child.restorationIdentifier == "LastestPostsNavigationController" { let lastestPostsTableViewController = (child.childViewControllers[0]) as! LastestPostsTableViewController let simplePostVC = (self.storyboard?.instantiateViewControllerWithIdentifier("PostViewController"))! as! PostViewController simplePostVC.post = post lastestPostsTableViewController.navigationController?.pushViewController(simplePostVC,animated: true) } } 我用 : child.childViewControllers[0] 因为我的例子里只有一个孩子. 解决方法
我创建了一个带有本地通知而不是远程通知的示例项目,以便于显示功能,但它应该像在app delegate didreceiveremote通知中设置窗口的根视图控制器一样简单.
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Subscribe for notifications - assume the user chose yes for now application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound],categories: nil)) return true } func applicationDidEnterBackground(application: UIApplication) { //Crete a local notification let notification = UILocalNotification() notification.alertBody = "This is a fake notification" notification.fireDate = NSDate(timeIntervalSinceNow: 2) UIApplication.sharedApplication().scheduleLocalNotification(notification) } func application(application: UIApplication,didReceiveLocalNotification notification: UILocalNotification) { let sb = UIStoryboard(name: "Main",bundle: nil) let otherVC = sb.instantiateViewControllerWithIdentifier("otherVC") as! OtherViewController window?.rootViewController = otherVC; } func application(application: UIApplication,didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { //Your code here } ` 在我的示例中,当您关闭在视图秒后触发的应用程序时,我会创建本地通知.如果您随后从通知中启动应用程序,它将打开“其他视图控制器”,在您的情况下将是“SimplePostViewController”. 另外,请确保在didFinishLaunchWithOptions中注册远程通知. Github非常简单的样本:https://github.com/spt131/exampleNotificationResponse (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Cocos2d-x_CCLabelProtocol(标签类)
- 在ruby中如何使用类级别的局部变量? (一个红宝石新手的问题
- 如何在C/C++中执行RGB-> YUV转换?
- 使用SQLite中自带的API操作SQLite数据库
- 纯前端表格控件SpreadJS V11.2新版本发布,全面支持React和
- ruby-on-rails – 如何在使用多对多关系时插入行
- Oracle dataguard 正常切换和应急切换
- build.xml:278: Unable to find a javac compiler;
- react native Image 控件显示图片方式总结
- 模态对话框 – 语义UI模态和ajax加载的内容