2

I am receiving a remote notification. I want to post a nsnotification. I can post the notification, but I can't receive it in my current view controller.

Here is my code.

I know this gets called:

AppDelegate.m

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

 [[NSNotificationCenter defaultCenter] postNotificationName:CHAT_MESSAGE_RECEIVED object:nil userInfo:messageIdDict];

 }

This gets called before the above code gets called

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");
 }
4

1 回答 1

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");
 }

希望这可以帮助

于 2013-07-12T08:11:43.910 回答