我有一个城市飞艇帐户,我的iOS应用程序已经配置了系统。我的设备令牌在服务器上,如果我从服务器发送测试通知,我会在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或其他东西发送???
答案 0 :(得分:0)
正如http://docs.urbanairship.com/reference/api/v3/intro.html上的文档所述,请求是带有JSON正文的http请求。
您需要先授权。来自文档:
用户名部分是应用程序密钥。密码部分是 无论是申请秘密还是主秘密。
您可以在此question中找到如何在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将字典序列化为字符串
答案 1 :(得分:0)
好的,这是我的实际方法:
- (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/>
我真的不知道为什么它不起作用!!!
答案 2 :(得分:0)
仅供参考,这样做会在公开发布的应用中带来风险。
任何UA推送都需要您的主密钥进行身份验证。如果您将主密钥添加到应用程序中,那么精明的黑客就可以查看,然后可以使用它来代表您发送未经授权的推送。它曾经发生在以前的人身上。
通常不建议这样做。
答案 3 :(得分:0)
这是使用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
}
}