2011-06-06 113 views
0

我正在开发具有phonegap的应用程序,但是我试图通过带NSUrlconnection的插件生成推送通知呼叫。城市飞艇推送通知问题

通知使用以下命令: curl -X POST -u“:”-H“Content-Type:application/json”--data'{“device_tokens”:[“”],“aps”:{ “警告”: “维克兰特说你好!”, “徽章”: “5”}}” https://go.urbanairship.com/api/push/

现在我下面代码是怎样SAME

NSString *URL = @"https://go.urbanairship.com/api/push/"; 
    NSMutableURLRequest *req = [[[NSMutableURLRequest alloc] init] autorelease]; 

    [req setURL:[NSURL URLWithString:URL]]; 


    [req setHTTPMethod:@"POST"]; 

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"application/json; boundary=%@",boundary]; 

    [req addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    NSMutableData *body = [NSMutableData data]; 

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"{\"device_tokens\": [\"<devce token>\"], \"aps\": {\"alert\": \"Vikrant say Hello!\",\"badge\": \"5\"}}"] dataUsingEncoding:NSUTF8StringEncoding]]; 

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 


    [req setHTTPBody:body]; 

    NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self]; 
    finished = NO; 
    finishedWithError = NO; 
    if(xmlData == nil) 
     [xmlData release]; 
    if(conn) 
    { 
     xmlData = [[NSMutableData alloc] retain]; 
     while(!finished) 
     { 
      [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 
     } 
    } 

所以,它是一个HTTPS网址服务器认证。所以我写了代表。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge 
{ 
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) 
    { 
     NSLog(@"Trust Challenge Requested!"); 
     [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 
     [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 

    } 
    else if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]) 
    { 
    NSLog(@"HTTP Auth Challenge Requested!"); 
     NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"<apikey>" password:@"<master key>" persistence:NSURLCredentialPersistenceForSession]; 
     [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 
     [credential release]; 
    } 


} 

问题是,连接不接受用户名和密码。因为打印输出下面

2000-01-01 11:07:09.-540 WIPT[500:307] From APN 
[Switching to thread 13059] 
2000-01-01 11:07:13.-986 WIPT[500:307] Trust Challenge Requested! 
2000-01-01 11:07:14.-82 WIPT[500:307] didReceiveResponse 
2000-01-01 11:07:14.-25 WIPT[500:307] connection 
2000-01-01 11:07:14.-05 WIPT[500:307] connectionDidFinishLoading 
2000-01-01 11:07:15.-958 WIPT[500:307] APN response data Authorization Required 

这意味着它执行的URL,但不会发送用户名和密码。有谁知道解决

回答

3

推送API调用通常与主秘密作为密码,而不是应用秘密认证。考虑应用程序的秘密是一个可以安全地嵌入到应用程序中的受限访问代码;你永远不会在应用程序中嵌入主密钥。

但是,为了使推送呼叫的某个子集可用而不需要主密钥,您可以启用允许推送来自Urban Airship应用的设备标志。这可让您直接将推送电话发送至具有应用程序密钥的设备令牌。它不会允许您推送别名,标签或进行全面广播,因为这些可能会被猜出或者会花费您很多麻烦。

亚当
城市飞艇

+0

感谢您的回复亚当。我已经有“允许从设备推送”启用。我想这是编程错误。 – 2011-06-08 05:55:06

1

canAuthenticateAgainstProtectionSpace委托更换NSURLAuthenticationMethodServerTrustNSURLAuthenticationMethodHTTPBasic

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]; 
}