从远程通知打开一个 ViewController [英] Open a ViewController from remote notification

查看:19
本文介绍了从远程通知打开一个 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用收到远程通知时,我尝试打开一个特定的 ViewController.

I try to open a particular ViewController when my app catch a remote notification.

让我展示一下我的项目的架构.这是我的故事板:

Let me show my project's architecture. Here my storyboard :

当我收到通知时,我想打开一个SimplePostViewController",所以这是我的 appDelegate :

When I receive a notification I want open a "SimplePostViewController", so this is my 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.但什么也没有发生.我放置了断点并看到我的 pushViewController 方法已到达,但没有到达我的 SimplePostViewController 的 ViewWillAppear.

I save my UINavigationViewController when the app is launch and simply try to push a new SimplePostViewController when I receive a notification. But nothing happen. I placed breakpoints and seen that my pushViewController method was reached, but not the ViewWillAppear of my SimplePostViewController.

我还使用了whats new"视图添加执行我的转场,但也没有任何反应.

I also used the "whats new" view add perform my segue but nothing happen too.

解决方案:

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]

因为在我的例子中我只有一个孩子.

because I've only one child in my example.

推荐答案

我创建了一个带有本地通知而不是远程通知的示例项目,以便于显示功能,但它应该像设置根视图控制器一样简单应用程序委托中的窗口确实收到了远程通知.

I created a sample project with a local notification instead of a remote notification for ease of showing the functionality but it should be as simple as setting the root view controller of the window in the app delegate didreceiveremote notification.

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
}

`您需要担心管理您的视图层次结构并将您需要从通知用户数据发送的任何内容发送给它.

` You need to worry about managing your view hierarchy and sending anything to it that you need to send from the notification user data.

在我的示例中,当您关闭查看秒后触发的应用程序时,我会创建一个本地通知.如果您然后从通知启动应用程序,它将打开其他视图控制器",在您的情况下将是SimplePostViewController".

In my example, I create a local notification when you close the app that fires after a view seconds. If you then launch the app from the notification, it will open the "other view controller" which would be the "SimplePostViewController" in your case.

另外,请确保您在 didFinishLaunchWithOptions 中注册了远程通知.

Also, be sure that you are registering for remote notifications in the didFinishLaunchWithOptions.

Github 非常简单的示例:https://github.com/spt131/exampleNotificationResponse

Github very simple sample : https://github.com/spt131/exampleNotificationResponse

这篇关于从远程通知打开一个 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆