在didReceiveRemoteNotification中显示子视图?

时间:2014-08-27 16:38:24

标签: objective-c xcode push-notification apple-push-notifications appdelegate

我尝试做的是在应用收到通知并且正在运行时在didReceiveRemoteNotification中显示我的子视图。我怎么能这样做?

我的代码现在在didReceiveRemoteNotification:

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive)
    {
        AcceptAlertViewCreator *acceptAlertViewCreator = [[AcceptAlertViewCreator alloc] init];
        //Here I try to get the current viewController running...
        UIViewController *viewController = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
        //This line gives me the "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView view]: unrecognized selector sent to instance 0x17816b7c0" error
        [viewController.view addSubview:[acceptAlertViewCreator createAlertViewWithViewController:viewController andText:[[userInfo objectForKey:@"aps"] objectForKey:@"message"]]];
    }
    else
    {
        //other things
    } 

我的AcceptAlertViewCreator会返回UIView并获得viewController的{​​{1}}以及viewController中的NSString消息。我的AcceptAlertViewCreator在播放时也有UIViewAnimation

我的AcceptAlertViewCreator在添加到普通ViewController subView后效果很好。

任何人都知道如何才能做到这一点? Id不必添加子视图。它可能在某些方面是一种解决方法,或者请给我一些指示。感谢

2 个答案:

答案 0 :(得分:0)

因此,当您收到警报时,您的AppDelegate会显示一个窗口。

我不会将其添加为子视图,我只会呈现视图控制器。我猜你在这个通知出来时想要采取一些行动。

当推送通知进入时,您还可以使用NSNotificationCenter向您的应用发送通知。您从didReceiveRemoteNotification触发NSNotification,您的应用视图需要侦听此通知。缺点是每个视图都需要监听此通知。

答案 1 :(得分:0)

而不是从viewController获取sharedApplication而不是rootViewController window

didReceiveRemoteNotification方法应如下所示:

UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
    AcceptAlertViewCreator *acceptAlertViewCreator = [[AcceptAlertViewCreator alloc] init];
    [self.window.rootViewController.view addSubview:[acceptAlertViewCreator createAlertViewWithViewController:self.window.rootViewController andText:[NSString stringWithFormat:@"%@", [[userInfo objectForKey:@"aps"] objectForKey:@"message"]]]];
}
else
{
    //other things
}