【问题标题】:how to send push from iphone via urban airship?如何通过城市飞艇从iphone发送推送?
【发布时间】:2013-11-16 23:59:16
【问题描述】:

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

【问题讨论】:

    标签: ios iphone xcode push urbanairship.com


    【解决方案1】:

    正如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 将您的字典序列化为字符串

    【讨论】:

      【解决方案2】:

      好的,这实际上是我的方法:

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

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

      【讨论】:

      • 你做错了授权,你需要发送第一个带有用户名的请求并通过,得到 OK 状态的响应,然后发送另一个请求到api/push。此外,如果您获得更多信息,您应该更新问题,而不是写答案
      • 好吧,现在我不确定,也许,你可以这样授权。无论如何,记录服务器响应NSData *response = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil]; NSString *responseStr = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
      • -[UAUserAPIClient updateDeviceToken:forUsername:onSuccess:onFailure:] 更新设备令牌。 -[UADeviceAPIClient registerWithData:onSuccess:onFailure:forcefully: 运行设备注册。 -[UAUser updateDefaultDeviceToken]_block_invoke 设备令牌更新为: -[UAPush updateRegistrationForcefully:]_block_invoke 设备令牌在 Urban Airship 上注册成功。 +[UAKeychainUtils getDeviceID] 从钥匙串中检索设备 ID 信息。 +[UAKeychainUtils getDeviceID] 加载的设备 ID:myID +[UAKeychainUtils getDeviceID] 加载的型号名称:iPhone3,1
      • 这不是服务器响应。您正在向 Urban Airship 服务器发送请求,它应该给您响应(在我的代码中它是响应字符串)
      • 对不起,我真的不明白。有好的教程或例子吗?
      【解决方案3】:

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

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

      一般不推荐。

      【讨论】:

        【解决方案4】:

        这是一个使用 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
        
        }
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多