如何通过城市飞艇从iPhone发送推力? [英] how to send push from iphone via urban airship?

查看:65
本文介绍了如何通过城市飞艇从iPhone发送推力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个城市飞艇帐户,并且我的iOS应用程序ist已经配置了该系统.我的设备令牌在服务器上,如果我从服务器发送测试通知,则会在iphone上收到通知.但是如何从iPhone向其他用户发送消息?我还没有发现有关通过iPhone发送的任何信息.这是我的代码:

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

然后我必须将其作为json或其他方式发送?

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

推荐答案

这是使用API​​ V3的示例

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

}

响应:

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

}

}

这篇关于如何通过城市飞艇从iPhone发送推力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆