objective-c - 接收远程推送通知时打开 View Controller

标签 objective-c ios apple-push-notifications

我正在使用 storyboard ,我想在用户收到远程推送通知时始终打开相同的 View ,即使应用程序在后台或已打开。我需要呈现的 View 是 Storyboard中设置的初始 View Controller 之后的四个 View 。我读了这篇文章:

How can I show a modal view in response to a notification as a new window? (no parent vc)

Open a specific tab/view when user receives a push notification

这是我的代码:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
    notificacionViewController *menu = [navController.storyboard instantiateViewControllerWithIdentifier:@"notificacion"];

    // First item in array is bottom of stack, last item is top.
    navController.viewControllers = [NSArray arrayWithObjects:menu,nil];

    [self.window makeKeyAndVisible];


}

但是当我收到通知时,应用程序崩溃并出现此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[locationViewController setViewControllers:]: unrecognized selector sent to instance 0x42ccd0'

locationViewController 是 Storyboard中设置为初始的 View Controller 。

非常感谢。

最佳答案

请尝试以下代码:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
    NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];
    [navController.visibleViewController.navigationController pushViewController:notificationViewController];    
}

关于objective-c - 接收远程推送通知时打开 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13416745/

相关文章:

swift - 当应用程序处于未运行状态时推送通知委托(delegate)触发的解决方案是什么?

iphone - 使用 iPhone 打印和保存网页

ios - MFMessageComposeViewController 取消按钮不起作用

iphone - 将 NSString 中的 ""字符替换为 "\"(以创建 Unix 路径)

ios - 无法将 AKAudioFile 加载到 AKSampler 中

c# - DidReceiveRemoteNotification 未在后台调用 - Xamarin iOS - 推送通知

iOS:当应用程序处于前台时,如何让用户忽略远程通知?

Objective-C 消息传递系统与微软的 WinAPI 消息传递系统

ios - 无法在 iOS 应用程序中使用 EventKit 添加事件

ios - 在 collectionview 单元格内的 ImageView 变得可见后对其进行动画处理