ios - Passing data received from a push notification from the AppDelegate to a ViewController -
in app, there's collection view displaying set of images retrieved web service. each image has tags. app has ability filter images using tags well.
now i'm trying add push notifications app. push notification sent when new images have been added server. these images tagged say, latest. i'm passing tag message via push notification , need when user taps on push notification open app, should load latest new images collection view.
i'm half way done. receive push notification message didreceiveremotenotification
method in appdelegate.m
file. need pass on view controller collection view is. i'm stuck @ point. can't figure out how send on view controller.
i tried declaring property in app delegate, assign message value , referring view controller didn't work. tied delegates, notification center, user defaults nothing worked.
can please tell me how accomplish this?
thank you.
edit:
here's code. last method tried local notifications.
appdelegate.m
- (void)application:(uiapplication *)application didreceiveremotenotification:(nsdictionary *)userinfo { [[nsnotificationcenter defaultcenter] postnotificationname:@"pushnotificationmessagereceivednotification" object:nil userinfo:userinfo]; }
viewcontroller.m
- (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(remotenotificationreceived:) name:@"pushnotificationmessagereceivednotification" object:nil]; } - (void)remotenotificationreceived:(nsnotification *)notification { nslog(@"notification: %@", notification.userinfo); nsstring *msg = [[notification.userinfo valueforkey:@"aps"] valueforkey:@"alert"]; self.label.text = msg; }
case 1: if app background , user launches app notification click have check if app launched form notification or normal
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { self.window = [[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; nsdictionary *remotenotificationpayload = [launchoptions objectforkey:uiapplicationlaunchoptionsremotenotificationkey]; if (remotenotificationpayload) { [[nsnotificationcenter defaultcenter] postnotificationname:@"notification" object:nil userinfo:remotenotificationpayload]; } return yes; }
case2: if app in forground notification received in didreceiveremotenotification
- (void)application:(uiapplication *)application didreceiveremotenotification:(nsdictionary *)userinfo { nslog(@"userinfo %@",userinfo); [[nsnotificationcenter defaultcenter] postnotificationname:@"notification" object:nil userinfo:userinfo]; }
now , add observer in controller local notification , wand
Comments
Post a Comment