3

I'm working on a simple prototype and need to test sending push notifications from one device to another.

I've emailed Urban Airship to turn on the "Allow Push From Device" for my application - and they did turn it on.

I'm trying to use NSURLConnection to send the push notification from the device.

This is my code:

- (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]) {
        NSURLCredential * credential = [[NSURLCredential alloc] initWithUser:@"<app key>" password:@"<app secret>" persistence:NSURLCredentialPersistenceForSession];
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
        [credential release];
    }
}

- (void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response {
    NSHTTPURLResponse * res = (NSHTTPURLResponse *) response;
    NSLog(@"response: %@",res);
    NSLog(@"res %i\n",res.statusCode);
}

Anyone else done this successfully?

gngrwzrd
  • 5,902
  • 4
  • 43
  • 56
  • And what happens when this code runs? What are you expecting to happen? – Sixten Otto Sep 26 '12 at 04:24
  • Ah forgot to mention that. The Response is a 405. Authentication Required. – gngrwzrd Sep 26 '12 at 17:27
  • @gngrwzrd : When I use your method, I get response as 401. What I am missing? In your code I just changed and . Do I need to do any changes in test method also?? – Fahim Parkar Mar 22 '13 at 20:36
  • @gngrwzrd : I also added my token id from UA instead of ``. Still I am not getting push for that. – Fahim Parkar Mar 22 '13 at 21:09
  • I also tried with `NSDictionary *push = @{@"aps": @{@"badge": @1, @"alert": @"wow... its working... i m so happy... with badge 1....", @"sound": @"default"}};` but still no luck – Fahim Parkar Mar 22 '13 at 21:28
  • I am confused... you said `password:@"" `, but URban doc says `curl -X POST -u ":"`... I confused with Master Secret... – Fahim Parkar May 26 '13 at 14:56

2 Answers2

6

Taking a look at Urban Airship's guide to troubleshooting HTTP status codes, and the documentation for the push API, my guess would be that you need to add a trailing slash to the URL:

[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]
Sixten Otto
  • 14,816
  • 3
  • 48
  • 60
2

Example Using the V3 API...

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

}

And The Response:

- (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

}

}

iDaniel89
  • 65
  • 6