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

これは 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

OKこれは私の実際の方法です:

- (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 に答える