退出应用程序时从远程通知显示 uiviewcontroller
Show uiviewcontroller from remote notification when app is quitted
我正在尝试在收到远程推送通知时显示特定的 viewcontroller。我已将所有代码添加到方法 didReceiveRemoteNotification
:
func application(application: UIApplication, didReceiveRemoteNotification userinfo: [NSObject: AnyObject])
我添加了以下代码:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
let code = (userInfo["aps"] as! [String: AnyObject])
// Call to retrieve blog
if let blog = code["b"] as? NSNumber {
let blogId = blog as! Int
// Show blog from notification
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("blogCtrl") as! BlogController
controller.blogId = blogId
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
if let tonic = code["t"] as? NSNumber {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("tonicDetail") as! TonicDetailController
controller.tonicId = tonic as! Int
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
if let gin = code["g"] as? NSNumber {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("GinDetail") as! GinDetailController
controller.ginId = gin as! Int
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
}
当应用程序在后台运行时,一切正常,但当应用程序退出时,我收到远程通知,它只会启动应用程序。
有没有什么方法可以在之前退出的时候调用?
当您的应用程序在退出应用程序后启动时,同时您收到远程通知,然后 didFinishLaunchingWithOptions
是 AppDelegate 中的第一个方法,它在启动应用程序时触发,请检查您是否收到是否有任何通知并相应地执行您的操作。
你必须在app delegate中寻找这个方法:-
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
现在检查您的应用是否收到任何推送通知
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
NSLog(@"app received a notification %@",notification);
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}else{
NSLog(@"app did not receive a notification");
}
我正在尝试在收到远程推送通知时显示特定的 viewcontroller。我已将所有代码添加到方法 didReceiveRemoteNotification
:
func application(application: UIApplication, didReceiveRemoteNotification userinfo: [NSObject: AnyObject])
我添加了以下代码:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
let code = (userInfo["aps"] as! [String: AnyObject])
// Call to retrieve blog
if let blog = code["b"] as? NSNumber {
let blogId = blog as! Int
// Show blog from notification
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("blogCtrl") as! BlogController
controller.blogId = blogId
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
if let tonic = code["t"] as? NSNumber {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("tonicDetail") as! TonicDetailController
controller.tonicId = tonic as! Int
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
if let gin = code["g"] as? NSNumber {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("GinDetail") as! GinDetailController
controller.ginId = gin as! Int
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
}
当应用程序在后台运行时,一切正常,但当应用程序退出时,我收到远程通知,它只会启动应用程序。 有没有什么方法可以在之前退出的时候调用?
当您的应用程序在退出应用程序后启动时,同时您收到远程通知,然后 didFinishLaunchingWithOptions
是 AppDelegate 中的第一个方法,它在启动应用程序时触发,请检查您是否收到是否有任何通知并相应地执行您的操作。
你必须在app delegate中寻找这个方法:-
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
现在检查您的应用是否收到任何推送通知
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
NSLog(@"app received a notification %@",notification);
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}else{
NSLog(@"app did not receive a notification");
}