如何通过城市飞艇从iPhone推送?

我有一个城市飞艇帐户,我的iOS应用程序已经configuration了系统。 我的设备令牌是在服务器上,如果我从服务器发送testing通知我得到我的iPhone上的通知。 但是我怎样才能发送消息从我的iPhone到另一个用户? 我还没有发现任何有关通过iPhone发送的东西。 这是我的代码:

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSDictionary *push; push = @{@"aps":@{@"alert":@"How are you?"},@"device_tokens":@[@"87892382J393FS77789S90909N82022312332"]}; 

然后我必须发送它为JSON或其他?

正如http://docs.urbanairship.com/reference/api/v3/intro.html上的文档所述,请求是带有JSON正文的http。

您需要先授权。 从文档:

用户名部分是应用程序密钥。 密码部分是应用程序密钥或主密钥。

你可以在这个问题中find如何在iOS上授权:

 NSString *authStr = [NSString stringWithFormat:@"%@:%@", [self username], [self password]]; NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding]; NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodingWithLineLength:80]]; [request setValue:authValue forHTTPHeaderField:@"Authorization"]; 

然后我想你应该这样做:

 #import "JSONKit.h" NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]]; request.HTTPMethod = @"POST"; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSDictionary *push = @{@"aps":@{@"alert":@"How are you?"},@"device_tokens":@[@"87892382J393FS77789S90909N82022312332"]}; request.HTTPBody = [push.JSONString dataUsingEncoding:NSUTF8StringEncoding]; [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil]; 

请注意,您需要JSONKit将字典序列化为string

确定这是我的方法:

 - (IBAction)sendPush:(id)sender { NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSString *authStr = [NSString stringWithFormat:@"app key:app secret"]; NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding]; NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedDataWithOptions:nil]]; [request setValue:authValue forHTTPHeaderField:@"Authorization"]; NSDictionary *push = @{@"aps":@{@"alert":@"How are you?"},@"device_tokens":@[@"87892382J393FS77789S90909N82022312332"]}; request.HTTPBody = [push.JSONString dataUsingEncoding:NSUTF8StringEncoding]; NSLog(@"push: %@",push); NSLog(@"request: %@",request); [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; } 

如果我评论6-10行,那么我得到的NSLog:

 2013-11-06 12:59:13.238 myApp[7273:907] push: { aps = { alert = "How are you?"; }; "device_tokens" = ( 87892382J393FS77789S90909N82022312332 ); } 2013-11-06 12:59:13.241 myApp[7273:907] request: <NSMutableURLRequest https://go.urbanairship.com/api/push/> 

我真的不知道为什么它不起作用!

仅供参考,这样做会在公开发布的应用程序中带来风险。

任何通过UA推送都需要您的主密钥进行身份validation。 如果您将主密钥添加到应用程序中,则可以向精明的黑客查看,黑客可以用它来代表您发送未经授权的推送。 它曾经发生过。

这通常不被推荐。

这是一个使用API​​ V3的例子

 -(void)richPushNotification{ NSDictionary *push = @{ @"audience" : @{ @"device_token" : deviceToken }, @"device_types" : @[ @"ios" ], @"notification" : @{ @"ios" : @{ @"alert":Message, @"sound":@"default", @"badge":@"auto", } }, @"message": @{ @"title": Message, @"body": @"<html><body><h1>blah blah</h1> etc...</html>", @"content_type": @"text/html", @"extra": @{ @"offer_id" : @"608f1f6c-8860-c617-a803-b187b491568e" } } }; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:@"application/vnd.urbanairship+json; version=3;" forHTTPHeaderField:@"Accept"]; NSString *authStr = [NSString stringWithFormat:@"%@:%@", appKey, appMasterSecret]; NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding]; NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]]; [request setValue:authValue forHTTPHeaderField:@"Authorization"]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:push options:0 // Pass 0 if you don't care about the readability of the generated string error:NULL]; request.HTTPBody = jsonData; [NSURLConnection connectionWithRequest:request delegate:self]; 

}

和回应:

 - (void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response { NSHTTPURLResponse * res = (NSHTTPURLResponse *) response; NSLog(@"response: %@",res); NSLog(@"res %li\n",(long)res.statusCode); if (res.statusCode == 202) { //Show Alert Message Sent }else{ //Handle Error } 

}