城市飞艇-用NSURLConnection发送推送信息[英] Urban Airship - Send Push with NSURLConnection

本文是小编为大家收集整理的关于城市飞艇-用NSURLConnection发送推送信息的处理方法,想解了城市飞艇-用NSURLConnection发送推送信息的问题怎么解决?城市飞艇-用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);
}

还有其他人成功完成了吗?

解决方案

看看Urban Airship的HTTP 状态代码故障排除指南,以及 推送 API 的文档,我猜你需要在 URL 中添加一个斜杠:

[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]

本文地址:https://www.itbaoku.cn/post/924451.html