【问题标题】:Urban Airship - Send Push with NSURLConnectionUrban Airship - 使用 NSURLConnection 发送推送
【发布时间】:2012-09-25 23:56:31
【问题描述】:

我正在开发一个简单的原型,需要测试从一台设备向另一台设备发送推送通知。

我已通过电子邮件向 Urban Airship 发送电子邮件,要求我为我的应用程序打开“允许从设备推送” - 他们确实打开了它。

我正在尝试使用 NSURLConnection 从设备发送推送通知。

这是我的代码:

- (void) test {
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    NSDictionary * push = @{@"device_tokens":@[@"<token>"], @"aps":@{@"alert":@"TEST", @"sound":@"default"}};
    NSData * pushdata = [NSJSONSerialization dataWithJSONObject:push options:0 error:NULL];
    [request setHTTPBody:pushdata];

    [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void) connection:(NSURLConnection *) connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *) challenge {
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]) {
        NSURLCredential * credential = [[NSURLCredential alloc] initWithUser:@"<app key>" password:@"<app secret>" persistence:NSURLCredentialPersistenceForSession];
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
        [credential release];
    }
}

- (void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response {
    NSHTTPURLResponse * res = (NSHTTPURLResponse *) response;
    NSLog(@"response: %@",res);
    NSLog(@"res %i\n",res.statusCode);
}

还有其他人成功完成了吗?

【问题讨论】:

  • 当这段代码运行时会发生什么?你期望发生什么?
  • 啊忘记提了。响应是 405。需要身份验证。
  • @gngrwzrd :当我使用你的方法时,我得到的响应是 401。我错过了什么?在您的代码中,我刚刚更改了 。我还需要对测试方法进行任何更改吗??
  • @gngrwzrd :我还从 UA 添加了我的令牌 ID,而不是 &lt;token&gt;。我仍然没有得到推动。
  • 我也试过NSDictionary *push = @{@"aps": @{@"badge": @1, @"alert": @"wow... its working... i m so happy... with badge 1....", @"sound": @"default"}};,但还是没有成功

标签: objective-c ios push urbanairship.com


【解决方案1】:

看看 Urban Airship 的 guide to troubleshooting HTTP status codesdocumentation for the push API,我猜你需要在 URL 中添加一个斜杠:

[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]

【讨论】:

    【解决方案2】:

    使用 V3 API 的示例...

    -(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
      • 2023-03-30
      相关资源
      最近更新 更多