Urban Airship - 使用NSURLConnection发送推送

gng*_*zrd 3 push objective-c urbanairship.com ios

我正在研究一个简单的原型,需要测试从一个设备向另一个设备发送推送通知.

我已通过电子邮件发送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);
}
Run Code Online (Sandbox Code Playgroud)

其他人成功完成了吗?

Six*_*tto 6

看看Urban Airship的HTTP状态代码疑难解答指南,以及推送API文档,我的猜测是你需要在URL中添加一个尾部斜杠:

[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]
Run Code Online (Sandbox Code Playgroud)