4

我嘗試在我的應用程序捕獲遠程通知時打開特定的ViewController。從遠程通知中打開一個ViewController

讓我來展示我的項目的架構。 這裏我的故事板: enter image description here

當我收到通知,我想開一個「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。但沒有發生。 我放置斷點,並看到我的pushViewController方法已達到,但不是我的SimplePostViewController的ViewWillAppear。

我也使用「whats new」視圖添加執行我的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] 

,因爲我已經在我的例子只有一個孩子。

+0

搜索「從故事板實例化視圖控制器」 – vikingosegundo

回答

8

我創建了一個帶有本地通知而不是遠程通知的示例項目,以方便顯示功能,但它應該像在應用程序委託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

+0

感謝您的例子,您共享偉大的代碼。我發現了一個幾乎類似的解決方案,我編輯了我的問題向您展示。 – Lilrom

+0

非常高興你知道了 - 你能接受答案嗎? –

+0

是的,很抱歉! – Lilrom