gpt4 book ai didi

objective-c - Urban Airship - 使用 NSURLConnection 发送推送

转载 作者:可可西里 更新时间:2023-11-01 06:25:16 27 4
gpt4 key购买 nike

我正在开发一个简单的原型(prototype),需要测试从一台设备向另一台设备发送推送通知。

我已经通过电子邮件向 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 的 guide to troubleshooting HTTP status codes , 和 documentation for the push API ,我的猜测是您需要在 URL 中添加尾部斜杠:

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

关于objective-c - Urban Airship - 使用 NSURLConnection 发送推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12592762/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com