NSNotificationCenter 和 didReceiveRemoteNotification
NSNotificationCenter and didReceiveRemoteNotification
我在使用 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 未加载。我不知道该怎么办。
您所附示例代码的当前流程是:
- 收到推送通知时,post 向 NSNotificationCenter 发出通知(与推送通知完全不同且无关)。
- 当加载另一个视图时(有人需要在收到推送通知之前加载此视图),订阅此 NSNotificationCenter 通知。
- 通知 post 后,将其打印到日志中。
我很难从你的问题中理解这一点,但如果你因为收到推送通知而尝试启动的视图控制器是 NewsDetailViewController,那么你的代码不会去做。您的代码所做的是打印出要记录的通知(假设其他人确保在收到推送通知之前加载了 NewsDetailViewController)。
为了在收到推送通知时加载 NewsDetailViewController,您不需要 post 通知 NSNotification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NewsDetailViewController *newsVC = [[NewsDetailViewController alloc] initWithNibName:@"NewsDetailViewController" bundle:nil];
[self.window.rootViewController.view addSubview:newsVC.view];
}
或任何其他更适合您的加载逻辑。但是在您 post 编辑的当前代码中,接收推送通知和加载 ViewController.
之间没有任何联系
希望对您有所帮助。祝你好运!
我在使用 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 未加载。我不知道该怎么办。
您所附示例代码的当前流程是:
- 收到推送通知时,post 向 NSNotificationCenter 发出通知(与推送通知完全不同且无关)。
- 当加载另一个视图时(有人需要在收到推送通知之前加载此视图),订阅此 NSNotificationCenter 通知。
- 通知 post 后,将其打印到日志中。
我很难从你的问题中理解这一点,但如果你因为收到推送通知而尝试启动的视图控制器是 NewsDetailViewController,那么你的代码不会去做。您的代码所做的是打印出要记录的通知(假设其他人确保在收到推送通知之前加载了 NewsDetailViewController)。
为了在收到推送通知时加载 NewsDetailViewController,您不需要 post 通知 NSNotification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NewsDetailViewController *newsVC = [[NewsDetailViewController alloc] initWithNibName:@"NewsDetailViewController" bundle:nil];
[self.window.rootViewController.view addSubview:newsVC.view];
}
或任何其他更适合您的加载逻辑。但是在您 post 编辑的当前代码中,接收推送通知和加载 ViewController.
之间没有任何联系希望对您有所帮助。祝你好运!