Reputation: 1604
I have this iOS app that receives push notification from server, in the push notification it contains a url. Now my app has a tabbar controller and 4 tabs, each tab contains a webview, how can I open the url and load the page in one of the webviews right after receiving push notification?
I was able to get the url from the notification message like this:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"userInfo: %@",[userInfo description]);
NSLog(@"alert: %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
NSLog(@"alert: %@",[[userInfo objectForKey:@"aps"] objectForKey:@"url"]);
}
But got stucked here as I can't figure out how to let the webview know it is time to load this url now. Note the push notification handling happens in appDelegate.m, but my webview is in another viewcontroller.
Upvotes: 2
Views: 1237
Reputation: 1604
Thanks to Paulw11, I've managed to solve it using NSNotification: In appDelegate.m:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedPushNotification" object:userInfo];
}
and in the view controller that needs to receive the notification:
- (void)viewDidLoad
{
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performTask:) name:@"ReceivedPushNotification" object:nil];
}
- (IBAction)performTask:(NSNotification *) notification
{
NSLog(@"notification received");
NSLog(@"%@", notification.object);
NSLog(@"alert: %@", [[notification.object objectForKey:@"aps"] objectForKey:@"url"]);
[self.webview_main loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[notification.object objectForKey:@"aps"] objectForKey:@"url"]]]];
}
Upvotes: 3