在推送通知 Swift 上加载特定 viewController 2 AppDelegate
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
。但我仍然看到相同的行为。
您的 RAMAnimatedTabBarController
继承了哪个 class?如果它继承了 UIViewController
/UITabbarController
然后取一个 UINavigationController
并将其 rootViewController
设置为您的 RAMAnimatedTabBarController
实例,然后将其分配为 window 的rootViewController 并为此 UINavigationController
设置隐藏的 navigationBar
并且当你想推送到特定控制器时这样做
var presentedVC = self.window?.rootViewController as? UINavigationController
presentedVC(yourdesiredController, animated: true)
我想回答我自己的问题,因为我想也许有人面临同样的情况,我不得不感谢@Muneeba
好吧,首先你必须知道你的远程推送通知是否需要进行后台获取,这很重要,因为如果是这样,didReceiveRemoteNotification
会被调用两次(第一次是当你点击通知警报时,打开应用程序的秒数),因此您必须注意这一点。
在我的例子中,我没有进行后台获取,所以我的 content-available:false
标志设置为 false。
接下来要做的是将您想要 ["key":value]
添加到通知负载中,以了解您是否要打开特定的视图控制器。
管理是否打开视图控制器。
在我的应用程序中,我想打开一个警报控件弹出窗口,始终显示推送通知的主体(带有一个确定按钮),并且仅当设置了特定值时才打开一个警报控件,向用户询问是否或不是他想打开到达的新内容(通常此内容是基于 Web 的内容,打开嵌入在视图控制器中的 Web 视图)
在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)
}
我想打开一个特定的视图控制器(一个隐藏的视图控制器,因为它只有在收到某个推送通知时才能访问(它不能通过标签栏访问))并且我已经能够做到这一点但是我'我遇到了问题..
我的应用有一个名为 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
。但我仍然看到相同的行为。
您的 RAMAnimatedTabBarController
继承了哪个 class?如果它继承了 UIViewController
/UITabbarController
然后取一个 UINavigationController
并将其 rootViewController
设置为您的 RAMAnimatedTabBarController
实例,然后将其分配为 window 的rootViewController 并为此 UINavigationController
设置隐藏的 navigationBar
并且当你想推送到特定控制器时这样做
var presentedVC = self.window?.rootViewController as? UINavigationController
presentedVC(yourdesiredController, animated: true)
我想回答我自己的问题,因为我想也许有人面临同样的情况,我不得不感谢@Muneeba
好吧,首先你必须知道你的远程推送通知是否需要进行后台获取,这很重要,因为如果是这样,didReceiveRemoteNotification
会被调用两次(第一次是当你点击通知警报时,打开应用程序的秒数),因此您必须注意这一点。
在我的例子中,我没有进行后台获取,所以我的 content-available:false
标志设置为 false。
接下来要做的是将您想要 ["key":value]
添加到通知负载中,以了解您是否要打开特定的视图控制器。
管理是否打开视图控制器。
在我的应用程序中,我想打开一个警报控件弹出窗口,始终显示推送通知的主体(带有一个确定按钮),并且仅当设置了特定值时才打开一个警报控件,向用户询问是否或不是他想打开到达的新内容(通常此内容是基于 Web 的内容,打开嵌入在视图控制器中的 Web 视图)
在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)
}