2

How do I pass the notification handling from the app delegate to the view controller when receiving push notification (didReceiveRemoteNotification)? This would be useful for showing an Alert on the view. releasing the view or anything else relevant.

Pseudo code example for AppDelegate.m:

- (void)application:(UIApplication*)application didReceiveRemoteNotification (NSDictionary*)userInfo{
NSLog(@"Received notification: %@", userInfo);
// Here send the userInfo or other data to the appropriate view controller for handling it there (for example showing an alert there) //
 }
Idan
  • 9,880
  • 10
  • 47
  • 76
LuZa
  • 450
  • 1
  • 6
  • 27
  • What's the purpose of passing the delegate? Perhaps there are better approaches. If you really need it, you should probably define a custom init function or property in your ViewControllerB for passing this object. – Tore Olsen Jul 31 '13 at 17:52
  • Umm so the that code example is in the app delegate right? Do you declare a `pushViewController:animated` method? I guess im kinda confused. – Andrew Jul 31 '13 at 17:52
  • sub class both viewControllerA and viewControllerB from the same superClass and then make that super class the delegate – A'sa Dickens Jul 31 '13 at 17:53
  • 1
    If you want to get the application delegate you could do `[[UIApplication sharedApplication] delegate]` – Andrew Jul 31 '13 at 17:54
  • you can't have 2 references in 1 property... just reset the delegate and it will shift to the new controller... – A'sa Dickens Jul 31 '13 at 17:56
  • i solved it: http://stackoverflow.com/questions/11574154/how-to-reload-the-tableview-when-i-am-getting-the-push-notification – LuZa Aug 01 '13 at 09:48

1 Answers1

3

There's really no reason to pass the didReceiveNotification around the application. It's intended to be handled once; that being said, I'm rather unsure of why you'd want to pass a delegate around.

If you want to push a view controller above everything else (I have no clue about your View hierarchy, so I have no idea if this is really something you would use), you could perhaps do something like:

[[self.window rootViewController] presentViewController:[[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil] animated:YES completion:^{}];

This code is just throwing a modal view on top of everything.

Or if for some reason, you do need to handle the notification in more places than just the application delegate, there's two things you can do:

Delegate Model

Create a new delegate protocol in the AppDelegate header, and set this to whatever handler you wish - the down side to this is (as has been mentioned above) is that only one object can listen to the delegate at a time

@protocol MyNotificationDelegate <NSObject>
@required
    -(void) applicationDidReceiveRemoteNotification: (NSDictionary*)userInfo;
@end

Post a Notification

As many objects as you like can listen for this notification; in the object you want to listen:

AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ReceivedNotification" object:appDel];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationReceived:) name:@"ReceivedNotification" object:appDel];

And add the function:

-(void)notificationReceived :(NSNotification *)localNot{
    NSLog(@"userInfo from push: %@",localNot.userInfo );
}

On your application delegate callback:

- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo{
    NSLog(@"Received notification: %@", userInfo);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedNotification" object:self userInfo:userInfo];
}
Idan
  • 9,880
  • 10
  • 47
  • 76
user352891
  • 1,181
  • 1
  • 8
  • 14