Urban Airship - 使用NSURLConnection发送推送 [英] Urban Airship - Send Push with NSURLConnection
问题描述
我正在制作一个简单的原型,需要测试从一个设备向另一个设备发送推送通知。
I'm working on a simple prototype and need to test sending push notifications from one device to another.
我已通过电子邮件发送Urban Airship打开允许推送设备为我的应用程序 - 他们确实打开它。
I've emailed Urban Airship to turn on the "Allow Push From Device" for my application - and they did turn it on.
我正在尝试使用NSURLConnection从设备发送推送通知。
I'm trying to use NSURLConnection to send the push notification from the device.
这是我的代码:
- (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?
推荐答案
看看Urban Airship的 HTTP状态代码疑难解答指南,以及推送API 的文档,我的猜测是你需要在URL上添加一个尾部斜杠:
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/"]
这篇关于Urban Airship - 使用NSURLConnection发送推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!