簡體   English   中英

在推送通知Swift 2 AppDelegate上加載特定的viewController

[英]Load a specific viewController on push notification Swift 2 AppDelegate

我想打開一個特定的視圖控制器(一個隱藏的視圖控制器,因為它只有在收到某個推送通知時才可以訪問(不能通過選項卡欄訪問)),我已經能夠做到這一點,但是我遇到了一個問題。 。

我的應用程序有一個名為RootViewController的自定義選項卡欄控制器。 當您單擊具有特殊值的通知時,它將顯示一個警報視圖,詢問用戶是否要打開通知。

通知觸發將特定的視圖控制器置於最前面,但問題是我不再有權訪問選項卡欄。

我不知道該如何實現。

這是我在AppDelegate.m代碼:

var presentedVC = self.window?.rootViewController as? UINavigationController
while (presentedVC!.navigationController != nil)  {
    presentedVC = presentedVC!.navigationController
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("PushNotificationView") as? NotTestViewController

destinationViewController?.url = self.url!
presentedVC?.presentViewController(destinationViewController!, animated: true, completion: nil)

})
)
alertCtrl.addAction(UIAlertAction(title: "Cancelar", style: .Destructive, handler: nil))

該代碼有效,但不適用於所需的行為。

有什么想法我想念的嗎?

非常感謝

編輯

我將RAMTabBarAnimationController更改為TabBarController因為RAMTabBarAnimationController不會從TabBarController繼承。 但是我仍然看到相同的行為。

我想回答我自己的問題,因為我認為也許有人正面臨着同樣的情況,我必須感謝@Muneeba

好吧,首先,您必須知道您的遠程推送通知是否需要進行后台獲取,這很重要,因為如果這樣,那么didReceiveRemoteNotification被調用兩次(第一次單擊通知警報,第二次打開應用程序),因此,您必須意識到這一點。

就我而言,我沒有進行后台獲取,因此我的content-available:false標志設置為false。

接下來要做的是將您想要的["key":value]到通知有效負載中,以了解您是否要打開特定的視圖控制器。

管理是否打開視圖控制器。

在我的應用中,我想打開一個警報控件,該彈出窗口始終顯示推送通知的主體(帶有“確定”按鈕),並且僅當設置了某些值時,才打開一個警報控件,詢問用戶是否要打開到達的新內容(通常,該內容是基於Web的內容,可以打開嵌入在視圖控制器中的Webview)

didReceiveRemoteNotification

if let mensaje = userInfo["m"] as? String {
// Here is where I know if the value is set in the notification payload
 let alertCtrl = UIAlertController(title: titulo, message: mensaje as String, preferredStyle: UIAlertControllerStyle.Alert)
 if url == nil {
   alertCtrl.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
 } else {
 alertCtrl.addAction(UIAlertAction(title: "Ver receta", style: .Default, handler: {
  action in

      let storyboard = UIStoryboard(name: "Main", bundle: nil)
      //self.window?.rootViewController = storyboard.instantiateViewControllerWithIdentifier("TabbedController") as! UITabBarController
      let navigationController = storyboard.instantiateViewControllerWithIdentifier("pushNotificationNavigation") as! UINavigationController
      let dVC:NotTestViewController = navigationController.topViewController as! NotTestViewController
      // This is for passing the URL to the view Controller
      dVC.url = self.url!
      self.window?.rootViewController?.presentViewController(navigationController, animated: true, completion: {})

      })
      )
      alertCtrl.addAction(UIAlertAction(title: "Cancelar", style: .Destructive, handler: nil))
   }
   // Find the presented VC...
   var presentedVC = self.window?.rootViewController
   while (presentedVC!.presentedViewController != nil)  {
      presentedVC = presentedVC!.presentedViewController
      }
      presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil)

      handler(UIBackgroundFetchResult.NoData)
 }

您的RAMAnimatedTabBarController繼承哪個類? 如果它繼承了UIViewController / UITabbarController則使用UINavigationController並將其rootViewController設置為您的RAMAnimatedTabBarController實例,然后將其分配為窗口的rootViewController,並將此UINavigationController隱藏的navigationBar設置為隱藏,並且當您要推送到特定Controller時,請像這樣

var presentedVC = self.window?.rootViewController as? UINavigationController
presentedVC(yourdesiredController, animated: true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM