0

我正在使用 UrbanAirship 向我的应用程序发送推送消息。我的设置适用于开发和生产。我需要将网页网址作为推送消息发送。当用户打开消息时,我希望它重定向到我添加的 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"
    }
}

我收到了警报消息,但它再次打开了应用程序而不是 url。您能建议我如何使用 url 发送推送消息并打开该 url 吗?

4

1 回答 1

1

有两种方法可以做到这一点

使用 safari(未经测试的代码)打开 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"]];
     [[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;

全屏,或者你想要的......

于 2012-07-30T14:34:32.257 回答