城市飞艇推送通知问题
我正在开发一个带有phonegap的应用程序,但是我试图通过带有NSUrlconnection的插件生成推送通知调用。
通知确实适用于以下命令 curl -X POST -u ":" -H "Content-Type: application/json" --data '{"device_tokens": [""], "aps": {"alert": "Vikrant 说你好!", “徽章”:“5”}}' https://go.urbanairship.com/api/push/
现在我正在尝试使用下面的代码进行相同的操作,
NSString *URL = @"https://go.urbanairship.com/api/push/";
NSMutableURLRequest *req = [[[NSMutableURLRequest alloc] init] autorelease];
[req setURL:[NSURL URLWithString:URL]];
[req setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"application/json; boundary=%@",boundary];
[req addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"{\"device_tokens\": [\"<devce token>\"], \"aps\": {\"alert\": \"Vikrant say Hello!\",\"badge\": \"5\"}}"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:body];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
finished = NO;
finishedWithError = NO;
if(xmlData == nil)
[xmlData release];
if(conn)
{
xmlData = [[NSMutableData alloc] retain];
while(!finished)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
因此,它是一个带有服务器身份验证的HTTPS url。所以我已经写信给代表了。
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
NSLog(@"Trust Challenge Requested!");
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
else if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic])
{
NSLog(@"HTTP Auth Challenge Requested!");
NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"<apikey>" password:@"<master key>" persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
[credential release];
}
}
问题是,连接不接受用户名和密码。因为打印下面的输出
2000-01-01 11:07:09.-540 WIPT[500:307] From APN
[Switching to thread 13059]
2000-01-01 11:07:13.-986 WIPT[500:307] Trust Challenge Requested!
2000-01-01 11:07:14.-82 WIPT[500:307] didReceiveResponse
2000-01-01 11:07:14.-25 WIPT[500:307] connection
2000-01-01 11:07:14.-05 WIPT[500:307] connectionDidFinishLoading
2000-01-01 11:07:15.-958 WIPT[500:307] APN response data Authorization Required
意味着它执行 URL 但不发送用户名和密码。有谁知道解决办法
I am developing an app with phonegap, however I am trying to generate Push Notification call through plugin with NSUrlconnection.
Notification does work with following command
curl -X POST -u ":" -H "Content-Type: application/json" --data '{"device_tokens": [""], "aps": {"alert": "Vikrant say Hello!","badge": "5"}}' https://go.urbanairship.com/api/push/
NOW I AM TRYING SAME WITH BELOW CODE
NSString *URL = @"https://go.urbanairship.com/api/push/";
NSMutableURLRequest *req = [[[NSMutableURLRequest alloc] init] autorelease];
[req setURL:[NSURL URLWithString:URL]];
[req setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"application/json; boundary=%@",boundary];
[req addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"{\"device_tokens\": [\"<devce token>\"], \"aps\": {\"alert\": \"Vikrant say Hello!\",\"badge\": \"5\"}}"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:body];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
finished = NO;
finishedWithError = NO;
if(xmlData == nil)
[xmlData release];
if(conn)
{
xmlData = [[NSMutableData alloc] retain];
while(!finished)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
So, its a HTTPS url with server authentication. So i have written the delegates.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
NSLog(@"Trust Challenge Requested!");
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
else if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic])
{
NSLog(@"HTTP Auth Challenge Requested!");
NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"<apikey>" password:@"<master key>" persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
[credential release];
}
}
The problem is, connection is not accepting the username and password. because prints below output
2000-01-01 11:07:09.-540 WIPT[500:307] From APN
[Switching to thread 13059]
2000-01-01 11:07:13.-986 WIPT[500:307] Trust Challenge Requested!
2000-01-01 11:07:14.-82 WIPT[500:307] didReceiveResponse
2000-01-01 11:07:14.-25 WIPT[500:307] connection
2000-01-01 11:07:14.-05 WIPT[500:307] connectionDidFinishLoading
2000-01-01 11:07:15.-958 WIPT[500:307] APN response data Authorization Required
It means it executes the URL but dose not send Username and password. Does anybody know the solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
推送 API 调用通常使用主密钥作为密码进行身份验证,而不是应用程序密钥。将应用程序机密视为可以安全嵌入到应用程序中的受限访问代码;您永远不会将主密钥嵌入到您的应用程序中。
但是,要在没有主密钥的情况下使某些推送调用子集可用,您可以在 Urban Airship 应用程序上启用允许从设备推送标志。这使您可以使用应用程序密钥直接对设备令牌进行推送调用。它不允许您推送别名、标签或进行完整的广播,因为这些可能会被猜测或会给您带来很多麻烦。
亚当
城市飞艇
The push API calls are normally authenticated with the master secret as the password, not the application secret. Consider the application secret to be a restricted access code that can be safely embedded in the app; you'd never embed the master secret inside your application.
However, to make some subset of push calls available without the master secret, you can enable the allow push from device flag on the Urban Airship application. This lets you make push calls directly to a device token with the application secret. It will not allow you to make pushes to aliases, tags, or do full broadcasts, as these can be guessed or can cost you lots of trouble.
Adam
Urban Airship
将 canAuthenticateAgainstProtectionSpace 委托中的 NSURLAuthenticationMethodServerTrust 替换为 NSURLAuthenticationMethodHTTPBasic。
Replace NSURLAuthenticationMethodServerTrust with NSURLAuthenticationMethodHTTPBasic in canAuthenticateAgainstProtectionSpace delegate.