我收到了远程通知。我想发布一个nsnotification。我可以发布通知,但我无法在当前的视图控制器中收到它。
这是我的代码。
我知道这被称为:
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:CHAT_MESSAGE_RECEIVED object:nil userInfo:messageIdDict];
}
在调用上述代码之前调用它
MyViewController.m
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(messageReceived:)
name:CHAT_MESSAGE_RECEIVED
object:self];
}
-(void)messageReceived:(NSDictionary *)userInfo
{
NSLog(@"Logged");
}
答案 0 :(得分:1)
以下是我将如何做到这一点:
在AppDelegate中:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:CHAT_MESSAGE_RECEIVED
object:messageIdDict
userInfo:nil];
}
在MyViewController中:
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(messageReceived:)
name:CHAT_MESSAGE_RECEIVED
object:nil];
}
-(void)messageReceived:(NSNotification *)notification
{
NSDictionary *userInfo = NSDictionary dictionaryWithDictionary:[notification object]];
NSLog(@"Logged");
}
希望这有帮助