我在NSNotificationCenter和didReceiveRemoteNotification方面遇到了一些问题。当我收到APNS的新通知时,我想打开我的ViewController。在身体内部通知我有objectId - 它的关键。 我尝试将我的ViewController打开到didReceiveRemoteNotification但它无法正常工作((
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter]
postNotificationName:kDidReceiveRemoteNotification
object:userInfo];
}
NewsDetailViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didReceiveRemoteNotification:)
name:kDidReceiveRemoteNotification
object:nil];
}
- (void)didReceiveRemoteNotification:(NSNotification *)notification
{
NSLog(@"%s %@",__func__,[notification.userInfo description]);
}
Const.h
#define kDidReceiveRemoteNotification @"UIApplicationDidReceiveRemoteNotification"
未加载ViewController。我不知道该怎么做。
答案 0 :(得分:2)
您附加的示例代码的当前流程是:
我很难从你的问题中理解这一点,但如果你试图通过接收推送通知而启动的视图控制器是NewsDetailViewController,那么你的代码就不会这样做。您的代码所做的是打印通知以记录日志(假设其他人确保在收到推送通知之前加载了NewsDetailViewController)。
为了在收到推送通知时加载NewsDetailViewController,您无需将通知发布到NSNotification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NewsDetailViewController *newsVC = [[NewsDetailViewController alloc] initWithNibName:@"NewsDetailViewController" bundle:nil];
[self.window.rootViewController.view addSubview:newsVC.view];
}
或任何其他适合您的加载逻辑。但是在您发布的当前代码中,接收推送通知和加载ViewController之间没有任何关联。
我希望这会有所帮助。祝你好运!