我有这个iOS应用程序接收来自服务器的推送通知,在推送通知中它包含一个url。现在我的应用程序有一个标签栏控制器和4个标签,每个标签包含一个webview,如何在收到推送通知后立即打开网址并在其中一个网页浏览中加载页面?
我能够从通知消息中获取这样的网址:
- (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"]);
}
但是我被困在这里因为我无法弄清楚如何让webview知道现在是时候加载这个网址了。 请注意,推送通知处理发生在appDelegate.m中,但我的webview在另一个viewcontroller中。
答案 0 :(得分:3)
感谢Paulw11,我设法使用NSNotification解决了这个问题: 在appDelegate.m中:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedPushNotification" object:userInfo];
}
并在需要接收通知的视图控制器中:
- (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"]]]];
}