1

我有一个城市飞艇帐户,并且我的 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 或其他东西发送???

4

4 回答 4

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/>

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

于 2013-11-06T12:01:08.023 回答
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

}

}

于 2014-05-08T15:39:53.743 回答
0

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

通过 UA 进行的任何推送都需要您的主密钥进行身份验证。如果您将主密码添加到应用程序中,精明的黑客可以看到它,然后他们可以使用它代表您发送未经授权的推送。以前发生在人们身上。

一般不推荐。

于 2013-11-10T15:08:00.277 回答
0

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

您需要先授权。从文档:

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

您可以在这个问题中找到如何在 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 将您的字典序列化为字符串

于 2013-11-06T10:16:14.857 回答