【发布时间】:2012-09-25 23:56:31
【问题描述】:
我正在开发一个简单的原型,需要测试从一台设备向另一台设备发送推送通知。
我已通过电子邮件向 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 :当我使用你的方法时,我得到的响应是 401。我错过了什么?在您的代码中,我刚刚更改了
和 。我还需要对测试方法进行任何更改吗?? -
@gngrwzrd :我还从 UA 添加了我的令牌 ID,而不是
<token>。我仍然没有得到推动。 -
我也试过
NSDictionary *push = @{@"aps": @{@"badge": @1, @"alert": @"wow... its working... i m so happy... with badge 1....", @"sound": @"default"}};,但还是没有成功
标签: objective-c ios push urbanairship.com