how to send push from iphone via urban airship?

喜你入骨 提交于 2020-01-14 03:59:09

问题


I have an urban airship account and my iOS app ist already configured with the system. My device token is on the server and if I send a test notification from the server I get the notification on my iphone. BUT how can I send a message FROM my iphone to another user? I haven't found any stuff about sending through iphone. This is my code:

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"]};

and then I have to send it as json or something else???


回答1:


As documentation on http://docs.urbanairship.com/reference/api/v3/intro.html says, requests are http ones with JSON body.

You need to authorize first. From documentation:

The username portion is the application key. The password portion is either the application secret, or master secret.

You can find in this question how to authorize on 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"];

And then I guess you should do like this:

#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];

Note that you need JSONKit to serialize your dictionary into string




回答2:


OK this is my actually Method:

- (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];
}

If I comment the line 6-10 out then I get for the 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/>

I really don't know why it doesn't works!!!




回答3:


Just FYI, doing this would present a risk in a publicly released app.

Any push through UA requires your master secret to authenticate. If you added your master secret to the app, it would be viewable to a savvy hacker, who could then use it to send unauthorized pushes on your behalf. It has happened to people before.

It's not generally recommended.




回答4:


This is an Example using the 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];

}

And The Response:

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

}

}



来源:https://stackoverflow.com/questions/19800382/how-to-send-push-from-iphone-via-urban-airship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!