我正在使用UrbanAirship向我的应用程序发送推送消息。我的设置适用于开发和生产。 我需要发送web url作为推送消息。当用户打开消息时,我希望它重定向到我添加的网址。 我将此代码添加到了我的appdelegate。
`- (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"]);
}
并试图像
一样发送推送{
"aps":
{
"alert": "take a look at this site ",
"url": "www.mysite.com"
}
}
我收到了提醒信息但又打开了应用程序,而不是网址。 您能否告诉我如何使用网址发送推送消息并将其打开该网址?
答案 0 :(得分:1)
有两种方法可以做到这一点
使用safari(未测试代码)打开网址:
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo
{
NSLog(@"userInfo:%@",[userInfo description]);
NSLog(@"alert:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
NSLog(@"url:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"url"]);
webViewController.url = [NSURL URLWithString:[[userInfo objectForKey:@"aps"] objectForKey:@"url"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSURL URLWithString:[[userInfo objectForKey:@"aps"] objectForKey:@"url"]];
}
或者你必须在你的应用上处理它:
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo{
NSLog(@"userInfo:%@",[userInfo description]);
NSLog(@"alert:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
NSLog(@"url:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"url"]);
webViewController.url = [NSURL URLWithString:[[userInfo objectForKey:@"aps"] objectForKey:@"url"]];
}
例如,在你的WebViewController中需要以下方法
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
当然,在你的WebViewController.h上必须是
IBOutlet UIWebView *webView;
全屏,或者你想要的......