当应用程序退出时,如何从远程通知中显示UIViewController?

3

当我接收到远程推送通知时,我希望显示一个特定的视图控制器。我已经将所有代码添加到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()
    }
}

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

1个回答

2

当您的应用程序在退出后启动时,同时您收到了远程通知,则didFinishLaunchingWithOptions是AppDelegate中在启动应用程序时首先触发的方法,检查是否收到任何通知并根据需要执行操作。

您必须在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");
    }

在您的代码的第一行,您正在创建一个UILocalNotification,但是在第四行中,您将其用作NSDictionary。这正确吗?因为当我尝试运行您的代码时,我收到了一个错误: “var notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as?UILocalNotification 如果通知!= nil { println(“有一个通知”) self.application(application,didReceiveRemoteNotification:notification as!NSDictionary)}” 错误说:“NSDictionary不会隐式转换为[NSObject:AnyObject]]”。 - Hannes Van den Berghe
2
@HannesVandenBerghe,不要使用'UILocalNotification',请使用'Dictionary',那样会起作用。 - Aanabidden
@Aanabidden 这确实是解决方案!谢谢! - Hannes Van den Berghe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接