Dirigible Urbano – Enviar Push con NSURLConnection

Estoy trabajando en un prototipo simple y necesito probar enviar notifications push de un dispositivo a otro.

Envié un correo electrónico a Urban Airship para activar el "Permitir empujar desde dispositivo" para mi aplicación, y lo encendieron.

Estoy intentando usar NSURLConnection para enviar la notificación de inserción desde el dispositivo.

Este es mi código:

- (void) test { NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push"]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSDictionary * push = @{@"device_tokens":@[@"<token>"], @"aps":@{@"alert":@"TEST", @"sound":@"default"}}; NSData * pushdata = [NSJSONSerialization dataWithJSONObject:push options:0 error:NULL]; [request setHTTPBody:pushdata]; [NSURLConnection connectionWithRequest:request delegate:self]; } - (void) connection:(NSURLConnection *) connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *) challenge { if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]) { NSURLCnetworkingential * cnetworkingential = [[NSURLCnetworkingential alloc] initWithUser:@"<app key>" password:@"<app secret>" persistence:NSURLCnetworkingentialPersistenceForSession]; [[challenge sender] useCnetworkingential:cnetworkingential forAuthenticationChallenge:challenge]; [cnetworkingential release]; } } - (void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response { NSHTTPURLResponse * res = (NSHTTPURLResponse *) response; NSLog(@"response: %@",res); NSLog(@"res %i\n",res.statusCode); } 

¿Alguien más ha hecho esto con éxito?

Echando un vistazo a la guía de Urban Airship para la solución de problemas de los códigos de estado de HTTP y la documentation para la API de inserción , creo que debería agregar una barra inclinada a la URL:

 [NSURL URLWithString:@"https://go.urbanairship.com/api/push/"] 

Ejemplo de uso de la API V3 …

 -(void)richPushNotification{ NSDictionary *push = @{ @"audience" : @{ @"device_token" : deviceToken }, @"device_types" : @[ @"ios" ], @"notification" : @{ @"ios" : @{ @"alert":Message, @"sound":@"default", @"badge":@"auto", } }, @"message": @{ @"title": Message, @"body": @"<html><body><h1>blah blah</h1> etc...</html>", @"content_type": @"text/html", @"extra": @{ @"offer_id" : @"608f1f6c-8860-c617-a803-b187b491568e" } } }; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:@"application/vnd.urbanairship+json; version=3;" forHTTPHeaderField:@"Accept"]; NSString *authStr = [NSString stringWithFormat:@"%@:%@", appKey, appMasterSecret]; NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding]; NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]]; [request setValue:authValue forHTTPHeaderField:@"Authorization"]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:push options:0 // Pass 0 if you don't care about the readability of the generated string error:NULL]; request.HTTPBody = jsonData; [NSURLConnection connectionWithRequest:request delegate:self]; 

}

Y la respuesta:

 - (void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response { NSHTTPURLResponse * res = (NSHTTPURLResponse *) response; NSLog(@"response: %@",res); NSLog(@"res %li\n",(long)res.statusCode); if (res.statusCode == 202) { //Show Alert Message Sent }else{ //Handle Error } 

}