2012-09-25 32 views
3

我正在研究一个简单的原型,需要测试从一个设备向另一个设备发送推送通知。Urban Airship - 用NSURLConnection发送推送

我已经通过电子邮件发送Urban Airship来打开“允许从设备推送”我的应用程序 - 他们确实打开了它。

我想使用NSURLConnection从设备发送推送通知。

这是我的代码:

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

任何人都成功地做到了这一点?

+0

当这段代码运行时会发生什么?你期望发生什么? –

+0

啊忘了提到这一点。响应是405.需要身份验证。 – gngrwzrd

+0

@gngrwzrd:当我使用你的方法时,我的回答是401.我缺少什么?在你的代码中,我只是改变了。我是否需要对测试方法做任何更改? –

回答

1

示例使用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]; 

}

和响应:

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

} 

}

+0

这是最新版本的正确答案。 –

相关问题