我正在研究一個簡單的原型,需要測試從一個設備向另一個設備發送推送通知。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);
}
任何人都成功地做到了這一點?
當這段代碼運行時會發生什麼?你期望發生什麼? –
啊忘了提到這一點。響應是405.需要身份驗證。 – gngrwzrd
@gngrwzrd:當我使用你的方法時,我的回答是401.我缺少什麼?在你的代碼中,我只是改變了和。我是否需要對測試方法做任何更改? –