2011-06-06 4 views
0

Je suis en train de développer une application avec phonegap, mais j'essaie de générer un appel Push Notification via le plugin avec NSUrlconnection. La commande ne fonctionne pas avec la commande suivante: curl -X POST -u ":" -H "Type de contenu: application/json" --data "{" device_tokens ": [" "]," aps ": { "alerte": "Vikrant dire Bonjour!", "badge": "5"}} » https://go.urbanairship.com/api/push/Problème de notification push d'un dirigeable urbain

MAINTENANT JE SUIS eSSAIE MÊME aVEC lE CODE CI-DESSOUS

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]]; 
     } 
    } 

Ainsi, son URL HTTPS avec authentification du serveur. J'ai donc écrit les délégués.

- (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]; 
    } 


} 

Le problème est que la connexion n'accepte pas le nom d'utilisateur et le mot de passe. parce impressions ci-dessous la sortie

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 

Cela signifie qu'il exécute l'URL, mais pas la dose envoyer Nom d'utilisateur et mot de passe. Quelqu'un sait-il la solution

Répondre

3

Les appels API push sont normalement authentifiées avec le Maître secret comme mot de passe, pas le secret applicatif . Considérez le secret de l'application comme étant un code d'accès restreint qui peut être intégré en toute sécurité dans l'application; vous n'intégrerez jamais le secret principal dans votre application. Cependant, pour rendre certains sous-ensembles d'appels Push disponibles sans le secret maître, vous pouvez activer le drapeau permettant d'appuyer sur le périphérique dans l'application Urban Airship. Cela vous permet de faire des appels push directement sur un jeton de périphérique avec le secret de l'application. Il ne vous permettra pas d'effectuer des poussées vers des alias, des balises ou des diffusions complètes, car celles-ci peuvent être devinées ou peuvent vous coûter beaucoup d'ennuis.

Adam
Urban Airship

+0

Merci pour la réponse Adam. J'ai déjà "permettre push de l'appareil" activer. Je suppose que c'est un défaut de programmation. –

1

Remplacer NSURLAuthenticationMethodServerTrust avec NSURLAuthenticationMethodHTTPBasic dans canAuthenticateAgainstProtectionSpace délégué.

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