2011-06-06 174 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]; 
} 
相關問題