2015-05-21 95 views
3

我试图在接收远程推送通知时显示特定的viewcontroller。我已经添加了我所有的代码添加到方法didReceiveRemoteNotification当应用程序退出时显示远程通知的uiviewcontroller

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() 
    } 
} 

当应用程序在后台一切正常,但是当应用程序被quited,并我收到一个远程通知,它只启动应用程序。 如果应用程序之前被退出,是否有可以调用的方法?

回答

2

如果退出应用程序,在此期间你有远程通知,然后didFinishLaunchingWithOptions是在AppDelegate中第一种方法,其触发上启动应用程序,请检查您是否已经收到任何通知或不并据此执行您的操作后,你的应用程序启动。

你必须寻找这种方法在应用程序委托: -

-(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"); 
    } 
+0

在第一线你的代码创建了一个UILocalNotification,但是在第四行中,你将它用作NSDictionary。它是否正确? 因为当我试着你的代码我有一个错误: 'var notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? UILocalNotification 如果通知!= nil {“有一个通知”) self.application(application,didReceiveRemoteNotification:notification as!NSDictionary)}' Error说:'NSDictionary不能隐式转换为[NSObject:AnyObject] ]' –

+2

@HannesVandenBerghe而不是'UILocalNotification'使用'NSDictionary'将工作 – Aanabidden

+0

@Aababidden这确实是解决方案!谢谢! –

相关问题